1 From 5de4c1f8e0d5fb22a6d2927d3526498de2b21cbe Mon Sep 17 00:00:00 2001
2 From: Petr Nechaev <petr.nechaev@cogentembedded.com>
3 Date: Fri, 31 Jul 2015 20:22:36 +0300
4 Subject: [PATCH 7/9] [WORKAROUND] Allow amb-qt binding to work in case of
8 docs/amb.in.fidl | 21 ++++
9 plugins/dbus/amb-qt/CMakeLists.txt | 2 +-
10 plugins/dbus/amb-qt/ambqt.cpp | 237 ++++++++++++++++++++++++++-----------
11 plugins/dbus/amb-qt/ambqt.h | 59 ++++-----
12 plugins/dbus/amb-qt/ambqtquick.cpp | 22 +++-
13 plugins/dbus/amb-qt/ambqtquick.h | 22 +++-
14 plugins/dbus/amb-qt/qmldir | 1 +
15 plugins/dbus/amb-qt/test.cpp | 26 +++-
16 plugins/dbus/automotivemanager.cpp | 65 ++++++++++
17 9 files changed, 350 insertions(+), 105 deletions(-)
19 diff --git a/docs/amb.in.fidl b/docs/amb.in.fidl
20 index 398a33d..27d2827 100644
21 --- a/docs/amb.in.fidl
22 +++ b/docs/amb.in.fidl
23 @@ -220,6 +220,27 @@ interface org.automotive.Manager {
27 + * FindObjectsForZone
28 + * \brief find the DBus object path which matches "objectName" and "zone". This has the same behavior as FindObject
29 + * except that it takes an addition zone argument and only return the path for that zone.
30 + * \arg String property is the requested property to be retrieved.
31 + * \arg Zone zone is the zone which the object
32 + * \return list of object paths that provide the given object Name.
34 + * "org.automotive.Manager.InvalidZone"
35 + * "org.automotive.Manager.ObjectNotFound"
37 + method FindObjectsForZone {
43 + array of String objectPath
49 * \brief get a list of zones for a given objectName.
50 * \arg String objectName object name.
51 diff --git a/plugins/dbus/amb-qt/CMakeLists.txt b/plugins/dbus/amb-qt/CMakeLists.txt
52 index 2f2a2a8..42b7fb4 100644
53 --- a/plugins/dbus/amb-qt/CMakeLists.txt
54 +++ b/plugins/dbus/amb-qt/CMakeLists.txt
55 @@ -16,7 +16,7 @@ endif(Qt5Core_FOUND)
57 set(ambqt_headers ambqt.h amb-qt_global.h)
58 set(ambqt_sources ambqt.cpp)
59 -set(ambqtquick_sources ambqtquick.cpp)
60 +set(ambqtquick_sources ambqtquick.cpp qmldir)
61 set(ambqtquick_headers ambqtquick.h)
63 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs} ${QT_INCLUDE_DIRS})
64 diff --git a/plugins/dbus/amb-qt/ambqt.cpp b/plugins/dbus/amb-qt/ambqt.cpp
65 index 6a3faeb..c8f1faf 100644
66 --- a/plugins/dbus/amb-qt/ambqt.cpp
67 +++ b/plugins/dbus/amb-qt/ambqt.cpp
70 +Copyright (C) 2012 Intel Corporation
71 +Copyright (C) 2015 Cogent Embedded Inc.
73 +This library is free software; you can redistribute it and/or
74 +modify it under the terms of the GNU Lesser General Public
75 +License as published by the Free Software Foundation; either
76 +version 2.1 of the License, or (at your option) any later version.
78 +This library is distributed in the hope that it will be useful,
79 +but WITHOUT ANY WARRANTY; without even the implied warranty of
80 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
81 +Lesser General Public License for more details.
83 +You should have received a copy of the GNU Lesser General Public
84 +License along with this library; if not, write to the Free Software
85 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
88 #include <QDBusConnection>
89 #include <QDBusInterface>
93 -AmbProperty::AmbProperty(QString op, QString iface, QString propName)
94 - :QObject(), mPropertyName(propName),mInterfaceName(iface), mObjectPath(op),mDBusInterface(NULL),mZone(0)
95 +AmbProperty::AmbProperty(QString objectName, QString propName)
96 + :QObject(), mObjectName(objectName), mPropertyName(propName), mZone(0), managerIface(NULL)
99 + // Automatically connect when all properties have been set
104 -void AmbProperty::propertyChangedSlot(QString, QVariantMap values, QVariantMap )
105 +void AmbProperty::propertyChangedSlot(QString, QVariantMap values, QStringList )
107 - valueChanged(values);
108 + // try to get value
109 + mValue = values[mPropertyName];
111 + // try to parse time
112 + QVariant timeVariant = values["Time"];
114 + double NewTime = 0;
115 + if (timeVariant.isValid())
116 + NewTime = timeVariant.toDouble(&ok);
121 + valueChanged(mValue);
124 -void AmbProperty::propertyChanged1(QDBusVariant val, double t)
125 +void AmbProperty::connect()
128 - mValue = val.variant();
130 - signalChanged(mValue);
134 + managerIface = new QDBusInterface("org.automotive.message.broker","/","org.automotive.Manager", QDBusConnection::systemBus(), this);
136 + if(!managerIface->isValid())
138 + qWarning()<<"Failed to create manager interface";
139 + delete managerIface;
143 + QDBusReply<QList<QDBusObjectPath>> reply = managerIface->call("FindObjectsForZone", mObjectName, mZone);
145 + if(reply.isValid())
147 + for (int i=0; i < reply.value().size(); ++i)
149 + QDBusInterface* mDBusInterface = new QDBusInterface("org.automotive.message.broker",
150 + reply.value().at(i).path(),
151 + "org.freedesktop.DBus.Properties",
152 + QDBusConnection::systemBus(),
155 + if(!mDBusInterface->isValid())
157 + qDebug()<<"Failed to create dbus interface for property "<<propertyName();
158 + qDebug()<<"path: "<< reply.value().at(i).path();
159 + qDebug()<<"object: "<< objectName();
160 + qDebug()<<"property: "<< propertyName();
161 + qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
164 + // store object interface for future use
165 + ServerProperties.append(mDBusInterface);
167 + if(!QDBusConnection::systemBus().connect("org.automotive.message.broker",
168 + reply.value().at(i).path(),
169 + "org.freedesktop.DBus.Properties",
170 + "PropertiesChanged",
172 + SLOT(propertyChangedSlot(QString,QVariantMap,QStringList))))
174 + qDebug()<<"Failed to connect to signal";
175 + qDebug()<<"path: "<< reply.value().at(i).path();
176 + qDebug()<<"object: "<< objectName();
177 + qDebug()<<"property: "<< propertyName();
178 + qDebug()<<"Error: "<< QDBusConnection::systemBus().lastError().message();
183 + // if(mInterfaceName.isEmpty())
184 + // mInterfaceName = "org.automotive."+mPropertyName;
186 + // mDBusInterface = new QDBusInterface("org.automotive.message.broker",objectPath(), interfaceName(), QDBusConnection::systemBus(),this);
188 + // if(!mDBusInterface->isValid())
190 + // qDebug()<<"Failed to create dbus interface for property "<<propertyName();
191 + // qDebug()<<"Path: "<<objectPath();
192 + // qDebug()<<"Interface: "<<interfaceName();
193 + // qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
196 + // QString signalName = propertyName() + "Changed";
200 + // ///TODO: only use PropertiesChanged... treat AmbProperty like an object rather than a representation of just a single property in the object
202 + // if(!QDBusConnection::systemBus().connect("org.automotive.message.broker", objectPath(), mInterfaceName,
203 + // signalName, this, SLOT(propertyChanged1(QDBusVariant,double))))
205 + // qDebug()<<"Failed to connect to signal";
206 + // qDebug()<<"path: "<<objectPath();
207 + // qDebug()<<"interface: "<<interfaceName();
208 + // qDebug()<<"signal: "<<propertyName();
209 + // qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
213 -void AmbProperty::connect()
214 +void AmbProperty::disconnect()
218 - delete mDBusInterface;
221 - if(mObjectPath.isEmpty())
224 - if(mInterfaceName.isEmpty())
225 - mInterfaceName = "org.automotive."+mPropertyName;
227 - mDBusInterface = new QDBusInterface("org.automotive.message.broker",objectPath(), interfaceName(), QDBusConnection::systemBus(),this);
229 - if(!mDBusInterface->isValid())
231 - qDebug()<<"Failed to create dbus interface for property "<<propertyName();
232 - qDebug()<<"Path: "<<objectPath();
233 - qDebug()<<"Interface: "<<interfaceName();
234 - qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
237 - QString signalName = propertyName() + "Changed";
240 - if(!QDBusConnection::systemBus().connect("org.automotive.message.broker", objectPath(), "org.freedesktop.DBus.Properties",
241 - "PropertiesChanged", this, SLOT(propertyChangedSlot(QString,QVariantMap,QVariantMap))))
243 - qDebug()<<"Failed to connect to signal";
244 - qDebug()<<"path: "<<objectPath();
245 - qDebug()<<"interface: "<<interfaceName();
246 - qDebug()<<"signal: "<<propertyName();
247 - qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
250 - ///TODO: only use PropertiesChanged... treat AmbProperty like an object rather than a representation of just a single property in the object
252 - if(!QDBusConnection::systemBus().connect("org.automotive.message.broker", objectPath(), mInterfaceName,
253 - signalName, this, SLOT(propertyChanged1(QDBusVariant,double))))
255 - qDebug()<<"Failed to connect to signal";
256 - qDebug()<<"path: "<<objectPath();
257 - qDebug()<<"interface: "<<interfaceName();
258 - qDebug()<<"signal: "<<propertyName();
259 - qDebug()<<"Error: "<<QDBusConnection::systemBus().lastError().message();
261 + delete managerIface;
262 + managerIface = NULL;
264 + QDBusReply<QList<QDBusObjectPath>> reply = managerIface->call("FindObjectsForZone", mObjectName, mZone);
266 + if(reply.isValid())
268 + for (int i=0; i < reply.value().size(); ++i)
270 + QDBusConnection::systemBus().disconnect("org.automotive.message.broker",
271 + reply.value().at(i).path(),
272 + "org.freedesktop.DBus.Properties",
273 + "PropertiesChanged",
275 + SLOT(propertyChangedSlot(QString,QVariantMap,QStringList)));
279 + for (int i = 0; i < ServerProperties.size(); ++i)
280 + delete ServerProperties[i];
282 + ServerProperties.clear();
285 -void AmbProperty::getObjectPath()
286 +QVariant AmbProperty::updateValue()
288 - QDBusInterface managerIface("org.automotive.message.broker","/","org.automotive.Manager", QDBusConnection::systemBus(), this);
290 - if(!managerIface.isValid())
292 - qDebug()<<"Failed to create manager interface";
296 - QDBusReply<QDBusObjectPath> reply = managerIface.call("FindObjectForZone", mPropertyName, mZone);
297 + for (int i = 0; i < ServerProperties.size(); ++i)
299 + if (!ServerProperties[i]->isValid())
301 + qDebug()<<"error Interface is not valid "<<ServerProperties[i]->objectName();
304 + QVariant v = ServerProperties[i]->property(propertyName().toUtf8());
311 - if(reply.isValid())
313 - mObjectPath = reply.value().path();
315 +void AmbProperty::setValue(QVariant v)
317 + for (int i = 0; i < ServerProperties.size(); ++i)
319 + if (!ServerProperties[i]->isValid())
321 + qDebug()<<"error Interface is not valid "<<ServerProperties[i]->objectName();
324 + ServerProperties[i]->setProperty(propertyName().toUtf8(), v);
328 diff --git a/plugins/dbus/amb-qt/ambqt.h b/plugins/dbus/amb-qt/ambqt.h
329 index 3d7e793..6510daa 100644
330 --- a/plugins/dbus/amb-qt/ambqt.h
331 +++ b/plugins/dbus/amb-qt/ambqt.h
334 +Copyright (C) 2012 Intel Corporation
335 +Copyright (C) 2015 Cogent Embedded Inc.
337 +This library is free software; you can redistribute it and/or
338 +modify it under the terms of the GNU Lesser General Public
339 +License as published by the Free Software Foundation; either
340 +version 2.1 of the License, or (at your option) any later version.
342 +This library is distributed in the hope that it will be useful,
343 +but WITHOUT ANY WARRANTY; without even the implied warranty of
344 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
345 +Lesser General Public License for more details.
347 +You should have received a copy of the GNU Lesser General Public
348 +License along with this library; if not, write to the Free Software
349 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
354 @@ -17,57 +35,44 @@ class QDBusInterface;
355 class AmbProperty: public QObject
358 - Q_PROPERTY(QString propertyName READ propertyName WRITE setPropertyName)
359 + Q_PROPERTY(QString objectName READ objectName WRITE setObjectName)
360 + AUTOPROPERTY(QString, objectName, ObjectName)
361 + Q_PROPERTY(QString propertyName READ propertyName WRITE setPropertyName)
362 AUTOPROPERTY(QString, propertyName, PropertyName)
363 - Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
364 - Q_PROPERTY(QString interfaceName READ interfaceName WRITE setInterfaceName)
365 - AUTOPROPERTY(QString, interfaceName, InterfaceName)
366 - Q_PROPERTY(QString objectPath READ objectPath WRITE setObjectPath)
367 - AUTOPROPERTY(QString, objectPath, ObjectPath)
368 + Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
369 Q_PROPERTY(int zone READ zone WRITE setZone)
370 AUTOPROPERTY(int, zone, Zone)
371 Q_PROPERTY(double time READ time)
376 + AmbProperty(): mPropertyName("Value"), mZone(0), mTime(0), managerIface(NULL) { }
378 - AmbProperty():mDBusInterface(NULL),mZone(0),mTime(0) { }
380 - AmbProperty(QString op, QString iface, QString propName);
381 + AmbProperty(QString objectName, QString propName);
388 - void setValue(QVariant v)
390 - if(!mDBusInterface || !mDBusInterface->isValid())
392 - qDebug()<<"error Interface is not valid "<<interfaceName();
395 - mDBusInterface->setProperty(propertyName().toUtf8(), v);
397 + void setValue(QVariant v);
399 double time(){ return mTime; }
403 void valueChanged(QVariant val);
406 - void signalChanged(QVariant val);
410 + QVariant updateValue();
414 - void propertyChangedSlot(QString, QVariantMap values, QVariantMap);
415 - void propertyChanged1(QDBusVariant, double);
416 + void propertyChangedSlot(QString, QVariantMap values, QStringList);
419 - void getObjectPath();
420 - QDBusInterface* mDBusInterface;
421 + QList<QDBusInterface*> ServerProperties;
422 + QDBusInterface* managerIface;
426 diff --git a/plugins/dbus/amb-qt/ambqtquick.cpp b/plugins/dbus/amb-qt/ambqtquick.cpp
427 index c5102a9..4362297 100644
428 --- a/plugins/dbus/amb-qt/ambqtquick.cpp
429 +++ b/plugins/dbus/amb-qt/ambqtquick.cpp
432 +Copyright (C) 2012 Intel Corporation
433 +Copyright (C) 2015 Cogent Embedded Inc.
435 +This library is free software; you can redistribute it and/or
436 +modify it under the terms of the GNU Lesser General Public
437 +License as published by the Free Software Foundation; either
438 +version 2.1 of the License, or (at your option) any later version.
440 +This library is distributed in the hope that it will be useful,
441 +but WITHOUT ANY WARRANTY; without even the implied warranty of
442 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
443 +Lesser General Public License for more details.
445 +You should have received a copy of the GNU Lesser General Public
446 +License along with this library; if not, write to the Free Software
447 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
449 #include "ambqtquick.h"
450 #include <QtQml/qqml.h>
452 void Components::registerTypes(const char *uri)
454 - qmlRegisterType<AmbProperty>(uri,0,1,"AutomotivePropertyItem");
456 + Q_ASSERT(uri == QLatin1String("amb"));
457 + int ret = qmlRegisterType<AmbProperty>(uri, 0, 1,"AutomotivePropertyItem");
460 void Components::initializeEngine(QQmlEngine *, const char *)
461 diff --git a/plugins/dbus/amb-qt/ambqtquick.h b/plugins/dbus/amb-qt/ambqtquick.h
462 index a48104a..74b3d6d 100644
463 --- a/plugins/dbus/amb-qt/ambqtquick.h
464 +++ b/plugins/dbus/amb-qt/ambqtquick.h
467 +Copyright (C) 2012 Intel Corporation
468 +Copyright (C) 2015 Cogent Embedded Inc.
470 +This library is free software; you can redistribute it and/or
471 +modify it under the terms of the GNU Lesser General Public
472 +License as published by the Free Software Foundation; either
473 +version 2.1 of the License, or (at your option) any later version.
475 +This library is distributed in the hope that it will be useful,
476 +but WITHOUT ANY WARRANTY; without even the implied warranty of
477 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
478 +Lesser General Public License for more details.
480 +You should have received a copy of the GNU Lesser General Public
481 +License along with this library; if not, write to the Free Software
482 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
484 #include <QQmlExtensionPlugin>
487 @@ -7,8 +25,6 @@ class Components : public QQmlExtensionPlugin
488 Q_PLUGIN_METADATA(IID "org.automotive.qmlplugin")
492 void registerTypes(const char *uri);
493 - void initializeEngine(QQmlEngine *engine, const char *uri);
495 + void initializeEngine(QQmlEngine *engine, const char *uri);
497 diff --git a/plugins/dbus/amb-qt/qmldir b/plugins/dbus/amb-qt/qmldir
498 index a2d986c..49557ae 100644
499 --- a/plugins/dbus/amb-qt/qmldir
500 +++ b/plugins/dbus/amb-qt/qmldir
504 diff --git a/plugins/dbus/amb-qt/test.cpp b/plugins/dbus/amb-qt/test.cpp
505 index 6eb88eb..a11f5e4 100644
506 --- a/plugins/dbus/amb-qt/test.cpp
507 +++ b/plugins/dbus/amb-qt/test.cpp
510 +Copyright (C) 2012 Intel Corporation
511 +Copyright (C) 2015 Cogent Embedded Inc.
513 +This library is free software; you can redistribute it and/or
514 +modify it under the terms of the GNU Lesser General Public
515 +License as published by the Free Software Foundation; either
516 +version 2.1 of the License, or (at your option) any later version.
518 +This library is distributed in the hope that it will be useful,
519 +but WITHOUT ANY WARRANTY; without even the implied warranty of
520 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
521 +Lesser General Public License for more details.
523 +You should have received a copy of the GNU Lesser General Public
524 +License along with this library; if not, write to the Free Software
525 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
528 #include "timestamp.h"
530 @@ -15,7 +33,7 @@ int main(int argc, char** argv)
531 double totalLatency=0;
534 - QObject::connect(&speed, &AmbProperty::signalChanged,[&](QVariant val)
535 + QObject::connect(&speed, &AmbProperty::valueChanged,[&](QVariant val)
537 double t1 = speed.time();
538 double t2 = amb::currentTime();
539 @@ -26,10 +44,14 @@ int main(int argc, char** argv)
540 totalLatency+=latency;
542 DebugOut(0)<<"Average: "<<totalLatency / numSamples<<std::endl;
543 + qDebug()<<"Value: "<<val;
546 - speed.setPropertyName("VehicleSpeed");
547 + speed.setObjectName("VehicleSpeed");
548 + speed.setPropertyName("Speed");
551 + qDebug() << "Updated="<< speed.updateValue();
555 diff --git a/plugins/dbus/automotivemanager.cpp b/plugins/dbus/automotivemanager.cpp
556 index 64919e0..a164d11 100644
557 --- a/plugins/dbus/automotivemanager.cpp
558 +++ b/plugins/dbus/automotivemanager.cpp
559 @@ -14,6 +14,11 @@ static const gchar introspection_xml[] =
560 " <arg type='i' name='zone' direction='in'/>"
561 " <arg type='o' name='response' direction='out'/>"
563 + " <method name='FindObjectsForZone'>"
564 + " <arg type='s' name='searchstring' direction='in'/>"
565 + " <arg type='i' name='zone' direction='in'/>"
566 + " <arg type='ao' name='response' direction='out'/>"
568 " <method name='FindObjectForSourceZone'>"
569 " <arg type='s' name='searchstring' direction='in'/>"
570 " <arg type='s' name='source' direction='in'/>"
571 @@ -168,6 +173,66 @@ static void handleMethodCall(GDBusConnection *connection,
572 g_dbus_method_invocation_return_dbus_error(invocation,"org.automotive.Manager.InvalidZone", "zone not found");
575 + else if(method == "FindObjectsForZone")
580 + g_variant_get(parameters,"(si)", &arg, &zone);
582 + std::string propertyToFind = arg;
584 + if(propertyToFind == "")
586 + g_dbus_method_invocation_return_error(invocation,G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid argument.");
590 + std::list<AbstractDBusInterface*> interfaces = AbstractDBusInterface::getObjectsForProperty(propertyToFind);
592 + if(!interfaces.size())
594 + g_dbus_method_invocation_return_dbus_error(invocation, "org.automotive.Manager.ObjectNotFound", "Property not found");
598 + GVariantBuilder params;
599 + g_variant_builder_init(¶ms, G_VARIANT_TYPE_ARRAY);
601 + bool hasItems = false;
603 + for(auto itr = interfaces.begin(); itr != interfaces.end(); itr++)
605 + AbstractDBusInterface* t = *itr;
606 + if(!t->isSupported())
609 + if(t->zone() == (Zone::Type)zone)
613 + if(!t->isRegistered())
614 + t->registerObject();
616 + std::list<std::string> processes = manager->subscribedProcesses[t];
618 + if(!contains(processes,sender))
620 + DebugOut()<<"Referencing "<<t->objectPath()<<" with sender: "<<sender<<endl;
621 + manager->subscribedProcesses[t].push_back(sender);
624 + GVariant *newvar = g_variant_new("o",t->objectPath().c_str());
625 + g_variant_builder_add_value(¶ms, newvar);
630 + g_dbus_method_invocation_return_value(invocation, g_variant_new("(ao)",¶ms));
632 + g_dbus_method_invocation_return_dbus_error(invocation,"org.automotive.Manager.ObjectNotFound", "Property not found");
635 else if (method == "ZonesForObjectName")