version bump
[profile/ivi/wrt-plugins-ivi.git] / Vehicle.cpp
1 #include "Vehicle.h"
2 #include <gio/gio.h>
3 #include <stdexcept>
4 #include <list>
5
6 #include <Commons/ThreadPool.h>
7
8 #include <abstractpropertytype.h>
9 #include <vehicleproperty.h>
10
11
12 namespace DeviceAPI
13 {
14 namespace Vehicle
15 {
16         
17 using namespace WrtDeviceApis::Commons;
18
19 VehicleMaster::VehicleMaster()
20 :EventRequestReceiver<EventVehiclePropertyChanged>(ThreadEnum::APPLICATION_THREAD)
21 {
22 }
23
24
25
26 std::list<AbstractPropertyType *> VehicleMaster::get(std::string property)
27 {
28         GError* error = NULL;
29         GDBusProxy* managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
30                                                                                                                          "org.automotive.message.broker",
31                                                                                                                          "/",
32                                                                                                                          "org.automotive.Manager",
33                                                                                                                          NULL,
34                                                                                                                          &error);
35
36         if(error)
37         {
38                 throw std::runtime_error("could not get automotive message broker Manager interface");
39         }
40
41
42         GVariant* objectPath = g_dbus_proxy_call_sync(managerProxy, "findProperty", g_variant_new("(s)", property.c_str()), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
43
44         const char* objP = g_variant_get_string(objectPath, NULL);
45
46         if(std::string(objP) == "")
47         {
48                 throw std::runtime_error("could not find property "+ property);
49         }
50
51         GDBusProxy* propertiesProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
52                                                                                                                                  "org.automotive.message.broker",
53                                                                                                                                  objP,
54                                                                                                                                  "org.freedesktop.DBus.Properties",
55                                                                                                                                  NULL,
56                                                                                                                                  &error);
57         std::string interfaceName = "org.automotive." + property;
58
59         GVariant* propertyMap = g_dbus_proxy_call_sync(propertiesProxy, "GetAll", g_variant_new("(s)", interfaceName.c_str()), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
60
61         if(error)
62         {
63                 throw std::runtime_error("failed to call GetAll on interface " + interfaceName);
64         }
65
66         GVariantIter* iter;
67         char* key;
68         GVariant *value;
69
70         std::list<AbstractPropertyType*> returnValue;
71
72         g_variant_get(propertyMap,"(a{sv})",&iter);
73
74         while(g_variant_iter_next(iter,"{sv}", &key, &value))
75         {
76                 AbstractPropertyType* val = VehicleProperty::getPropertyTypeForPropertyNameValue(key);
77                 val->fromVariant(value);
78                 returnValue.push_back(val);
79         }
80
81         return returnValue;
82 }
83
84 void VehicleMaster::addEventListener(std::string property, EventVehiclePropertyChangedPtr& event)
85 {
86         
87 }
88
89 }
90 }