1 /* Copyright (C) 2015, Jaguar Land Rover. All Rights Reserved.
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <QCoreApplication>
8 #include <QDBusConnection>
9 #include <QDBusInterface>
10 #include <QDBusObjectPath>
15 int main(int argc, char *argv[])
17 // TODO: Clean this up, update API, etc.
18 if (argc < 5 || argc > 6)
20 qWarning() << "Error detected! Insufficient number of arguments.";
22 qWarning() << "Usage: ambdbusaccess <R/W> <Object> <Property> <Zone> [Value]";
23 qWarning() << "- <R/W>";
24 qWarning() << " Used for specifying [R]ead or [W]rite.";
25 qWarning() << "- <Object>";
26 qWarning() << " The AMB object to access.";
27 qWarning() << "- <Property>";
28 qWarning() << " The property within the object to access.";
29 qWarning() << "- <Zone>";
30 qWarning() << " The AMB zone to access.";
31 qWarning() << "- [Value]";
32 qWarning() << " The value to write, if writing.";
33 qWarning() << "Example: ambdbusaccess Write ClimateControl FanSpeedLevel 0 7";
35 qWarning() << "This program returns an int under the following conditions:";
36 qWarning() << "Successful Read: <Value Read>";
37 qWarning() << "Unsuccessful Read: -1";
38 qWarning() << "Successful Write: <Value Written>";
39 qWarning() << "Unsuccessful Write: -1";
44 // TODO: Error check input.
45 bool read = !strncmp(argv[1], "R", 1);
46 QString object = argv[2];
47 QString property = argv[3];
48 qint32 zone = atoi(argv[4]);
53 value = atoi(argv[5]);
56 // Necessary to suppress Qt messages about touching the D-Bus before the application was created.
57 QCoreApplication a(argc, argv);
59 // Sanity check that the system bus is actually present.
60 if (!QDBusConnection::systemBus().isConnected())
62 qCritical() << "Could not access system D-Bus!";
66 // Get ahold of the manager object.
67 QDBusInterface *manager = new QDBusInterface("org.automotive.message.broker", "/", "org.automotive.Manager",
68 QDBusConnection::systemBus());
70 // Go fetch the path for the AMB object we are concerned with.
71 qDebug().nospace() << "Looking for property " << property.toStdString().c_str() << " of object " <<
72 object.toStdString().c_str() << " in zone " << zone << ".";
73 QDBusReply<QDBusObjectPath> propertyPath = manager->call("FindObjectForZone", object.toStdString().c_str(), zone);
74 if (!propertyPath.isValid())
76 qDebug() << "Invalid reply!" << propertyPath.error() << "Got the path:" << propertyPath.value().path();
79 // Now that we have the path, open an interface to the object.
80 QDBusInterface *propertyInterface = new QDBusInterface("org.automotive.message.broker", propertyPath.value().path(),
81 "org.automotive.ClimateControl", QDBusConnection::systemBus());
83 // Perform our read or write operation.
86 QVariant reply = propertyInterface->property(property.toStdString().c_str());
89 qDebug() << "Invalid reply when reading the property!" << propertyInterface->lastError() << "Property:" <<
95 qDebug().nospace() << "Got a valid reply for the property of " << reply.toString().toStdString().c_str() << ".";
96 value = reply.toInt();
101 QVariant reply = propertyInterface->setProperty(property.toStdString().c_str(), value);
104 qDebug() << "Successfully wrote the property.";
108 qDebug() << "Failed to write the property.";
114 delete propertyInterface;
117 // Either provide the read value or some feedback to the calling application.