fixed frequency option in dbus plugin
[profile/ivi/automotive-message-broker.git] / plugins / dbus / dbusplugin.cpp
1 /*
2         Copyright (C) 2012  Intel Corporation
3
4         This library is free software; you can redistribute it and/or
5         modify it under the terms of the GNU Lesser General Public
6         License as published by the Free Software Foundation; either
7         version 2.1 of the License, or (at your option) any later version.
8
9         This library is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12         Lesser General Public License for more details.
13
14         You should have received a copy of the GNU Lesser General Public
15         License along with this library; if not, write to the Free Software
16         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <boost/lexical_cast.hpp>
20
21 #include "dbusplugin.h"
22 #include "abstractroutingengine.h"
23 #include "dbusinterfacemanager.h"
24 #include "debugout.h"
25 #include "listplusplus.h"
26
27 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
28 {
29         return new DBusSinkManager(routingengine, config);
30 }
31
32 std::map<std::string, std::string> DBusSink::dbusConfig;
33
34 DBusSink::DBusSink(string propertyName, AbstractRoutingEngine* engine, GDBusConnection* connection, map<string, string> config = map<string, string>())
35         :AbstractDBusInterface("org.automotive."+propertyName, propertyName, connection),
36           AbstractSink(engine, dbusConfig)
37 {
38         AbstractDBusInterface::re = engine;
39
40         int timeout = 60;
41
42         if(configuration.find("frequency") != configuration.end())
43         {
44                 int t = boost::lexical_cast<int>(configuration["frequency"]);
45
46                 timeout = 1000 / t;
47         }
48
49         setTimeout(timeout);
50 }
51
52 void DBusSink::supportedChanged(const PropertyList &supportedProperties)
53 {
54         startRegistration();
55
56         for(auto itr : propertyDBusMap)
57         {
58                 if(contains(supportedProperties, itr->ambPropertyName()))
59                 {
60                         VariantType* prop = itr;
61                         prop->setSourceFilter(mSourceFilter);
62                         prop->setZoneFilter(zoneFilter);
63                         prop->initialize();
64                         VehicleProperty::Property p = itr->ambPropertyName();
65                         routingEngine->subscribeToProperty(p, mSourceFilter, this);
66                         addProperty(prop);
67                         supported = true;
68                 }
69         }
70 }
71
72 void DBusSink::propertyChanged(AbstractPropertyType *value)
73 {
74         if(!isRegistered())
75                 return;
76
77         VehicleProperty::Property property = value->name;
78
79         if( value->zone != zoneFilter)
80                 return;
81
82         for(auto i : propertyDBusMap)
83         {
84                 if(i->ambPropertyName() == property)
85                 {
86                         AbstractProperty* prop = i;
87                         mTime = value->timestamp;
88                         prop->updateValue(value);
89                         updateValue(prop);
90                 }
91         }
92 }
93
94 const string DBusSink::uuid()
95 {
96         return "c2e6cafa-eef5-4b8a-99a0-0f2c9be1057d";
97 }
98
99 DBusSinkManager::DBusSinkManager(AbstractRoutingEngine *engine, map<string, string> config) :
100         AbstractSinkManager(engine, config),
101         manager(nullptr)
102 {
103         manager = new DBusInterfaceManager(engine, config);
104 }
105
106 DBusSinkManager::~DBusSinkManager()
107 {
108         if(manager){
109                 // delete manager; <-- currently AbstractSink* instances are deleted in Core::~Core()
110                 manager = nullptr;
111         }
112 }