6f2a7b8170772dda3d0992ebc29f85640798e646
[AGL/meta-agl-demo.git] / recipes-automotive / ambdbusaccess / files / ambdbusaccess.cpp
1 /* Copyright (C) 2015, Jaguar Land Rover. All Rights Reserved.
2  *
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/. */
6
7 #include <QCoreApplication>
8 #include <QDBusConnection>
9 #include <QDBusInterface>
10 #include <QDBusObjectPath>
11 #include <QDBusReply>
12 #include <QDebug>
13 #include <string.h>
14
15 int main(int argc, char *argv[])
16 {
17     // TODO: Clean this up, update API, etc.
18     if (argc < 5 || argc > 6)
19     {
20         qWarning() << "Error detected! Insufficient number of arguments.";
21         qWarning() << "";
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";
34         qWarning() << "";
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";
40
41         return -1;
42     }
43
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]);
49     qint32 value = 0;
50
51     if (argc == 6)
52     {
53         value = atoi(argv[5]);
54     }
55
56     // Necessary to suppress Qt messages about touching the D-Bus before the application was created.
57     QCoreApplication a(argc, argv);
58
59     // Sanity check that the system bus is actually present.
60     if (!QDBusConnection::systemBus().isConnected())
61     {
62         qCritical() << "Could not access system D-Bus!";
63         return -1;
64     }
65
66     // Get ahold of the manager object.
67     QDBusInterface *manager = new QDBusInterface("org.automotive.message.broker", "/", "org.automotive.Manager",
68                                                  QDBusConnection::systemBus());
69
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())
75     {
76         qDebug() << "Invalid reply!" << propertyPath.error() << "Got the path:" << propertyPath.value().path();
77     }
78
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());
82
83     // Perform our read or write operation.
84     if (read)
85     {
86         QVariant reply = propertyInterface->property(property.toStdString().c_str());
87         if (!reply.isValid())
88         {
89             qDebug() << "Invalid reply when reading the property!" << propertyInterface->lastError() << "Property:" <<
90                         reply.toString();
91             value = -1;
92         }
93         else
94         {
95             qDebug().nospace() << "Got a valid reply for the property of " << reply.toString().toStdString().c_str() << ".";
96             value = reply.toInt();
97         }
98     }
99     else
100     {
101         QVariant reply = propertyInterface->setProperty(property.toStdString().c_str(), value);
102         if (reply.toBool())
103         {
104             qDebug() << "Successfully wrote the property.";
105         }
106         else
107         {
108             qDebug() << "Failed to write the property.";
109             value = -1;
110         }
111     }
112
113     // Clean up.
114     delete propertyInterface;
115     delete manager;
116
117     // Either provide the read value or some feedback to the calling application.
118     return value;
119 }
120