added xwalk. added set
[profile/ivi/automotive-message-broker.git] / plugins / common / ambpluginimpl.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 <vehicleproperty.h>
20 #include <listplusplus.h>
21
22 #include "ambpluginimpl.h"
23
24 //----------------------------------------------------------------------------
25 // AmbPluginImpl
26 //----------------------------------------------------------------------------
27
28 AmbPluginImpl::AmbPluginImpl(AbstractRoutingEngine* re, const map<string, string>& config, AbstractSource& parent) :
29         source(parent),
30         routingEngine(re),
31         configuration(config)
32 {
33 }
34
35 void AmbPluginImpl::init()
36 {
37 }
38
39 void AmbPluginImpl::getPropertyAsync(AsyncPropertyReply *reply)
40 {
41         if(!reply) {
42                 DebugOut(DebugOut::Error) << "AmbPluginImpl::getPropertyAsync - reply is null" << std::endl;
43                 return;
44         }
45
46         DebugOut() << "AmbPluginImpl::getPropertyAsync called for property: " << reply->property << endl;
47
48         reply->success = false;
49         reply->error = AsyncPropertyReply::InvalidOperation;
50         AbstractPropertyType *value = findPropertyType(reply->property, reply->zoneFilter);
51         if (value) {
52                 reply->value = value;
53                 reply->success = true;
54                 reply->error = AsyncPropertyReply::NoError;
55         }
56
57         if(reply->completed)
58                 reply->completed(reply);
59 }
60
61 void AmbPluginImpl::getRangePropertyAsync(AsyncRangePropertyReply *reply)
62 {
63         if(!reply) {
64                 DebugOut(DebugOut::Error) << "AmbPluginImpl::getRangePropertyAsync - reply is null" << std::endl;
65                 return;
66         }
67
68         DebugOut() << "AmbPluginImpl::getRangePropertyAsync not supported "<< std::endl;
69         reply->success = false;
70         reply->error = AsyncPropertyReply::InvalidOperation;
71         if(reply->completed)
72                 reply->completed(reply);
73 }
74
75 AsyncPropertyReply *AmbPluginImpl::setProperty(const AsyncSetPropertyRequest& request )
76 {
77         AsyncPropertyReply* reply = new AsyncPropertyReply(request);
78         reply->success = false;
79         reply->error = AsyncPropertyReply::InvalidOperation;
80
81         AbstractPropertyType *value = findPropertyType(request.property, request.zoneFilter);
82         if (value && request.value) {
83                 DebugOut(2) << "updating property "<< request.property << " to: " << request.value->toString() << endl;
84                 value->fromString(request.value->toString());
85                 DebugOut(2) << "New value of property "<< request.property << " is: " << value->toString() << endl;
86                 value->timestamp = amb::currentTime();
87                 routingEngine->updateProperty(value, uuid());
88
89                 reply->success = true;
90                 reply->error = AsyncPropertyReply::NoError;
91         }
92
93         try {
94                 if(reply->completed)
95                         reply->completed(reply);
96         }
97         catch (...) { }
98
99         return reply;
100 }
101
102 void AmbPluginImpl::subscribeToPropertyChanges(const VehicleProperty::Property& property)
103 {
104         std::list<Zone::Type> zones = getPropertyInfo(property).zones();
105         for( auto it=zones.begin(); it!=zones.end(); ++it) {
106                 AbstractPropertyType *value = findPropertyType(property, *it);
107                 if(value)
108                         routingEngine->updateProperty(value, uuid());
109         }
110 }
111
112 PropertyList AmbPluginImpl::supported() const
113 {
114         PropertyList props;
115         for(auto itPropMap = properties.begin(); itPropMap != properties.end(); ++itPropMap)
116                 props.push_back(itPropMap->first);
117         return props;
118 }
119
120 int AmbPluginImpl::supportedOperations() const
121 {
122         return AbstractSource::Get | AbstractSource::Set;
123 }
124
125 void AmbPluginImpl::unsubscribeToPropertyChanges(const VehicleProperty::Property& property)
126 {
127 }
128
129 // if signal does not exits return nullptr(we do not know its datatype), if zone does not exists, add it
130 AbstractPropertyType* AmbPluginImpl::findPropertyType(const VehicleProperty::Property& propertyName, const Zone::Type& zone)
131 {
132         auto itPropMap = properties.find(propertyName);
133         if(itPropMap == properties.end())
134                 return nullptr;
135
136         for( auto it = itPropMap->second.begin(); it != itPropMap->second.end(); ++it ) {
137                 if(it->first == zone)
138                         return it->second.get();
139         }
140
141         return nullptr;
142 }
143
144 std::shared_ptr<AbstractPropertyType> AmbPluginImpl::addPropertySupport(Zone::Type zone, std::function<AbstractPropertyType* (void)> typeFactory)
145 {
146         std::shared_ptr<AbstractPropertyType> propertyType(typeFactory());
147         if(!propertyType)
148                 return propertyType;
149
150         VehicleProperty::Property name(propertyType->name);
151         PropertyList registeredProperties(VehicleProperty::capabilities());
152         bool registeredType(false);
153         if(!contains(registeredProperties,name))
154         {
155                 registeredType = VehicleProperty::registerProperty(name, typeFactory);
156         }
157         if(!registeredType){ // Property type wasn't registered by us. Is it predefined in AMB API or some other source plug-in has already registered it ???
158                 std::shared_ptr<AbstractPropertyType> registeredPropertyType(VehicleProperty::getPropertyTypeForPropertyNameValue(name));
159                 if(!registeredPropertyType)
160                         return nullptr;
161                 propertyType.swap(registeredPropertyType);
162         }
163         propertyType->zone = zone;
164         propertyType->sourceUuid = uuid();
165         ZonePropertyType& zonePropType = properties[name];
166         zonePropType.insert( make_pair(zone, propertyType));
167         return propertyType;
168 }
169
170 PropertyInfo AmbPluginImpl::getPropertyInfo(const VehicleProperty::Property& property) const
171 {
172         auto it = properties.find(property);
173         if(it != properties.end()) {
174                 std::list<Zone::Type> zones;
175                 for(auto itZonePropType = it->second.begin(); itZonePropType != it->second.end(); ++itZonePropType)
176                         zones.push_back(itZonePropType->first);
177
178                 return PropertyInfo( 0, zones );
179         }
180
181         return PropertyInfo::invalid();
182 }
183
184 void AmbPluginImpl::propertyChanged(AbstractPropertyType* value)
185 {
186 }
187
188 void AmbPluginImpl::supportedChanged(const PropertyList& supportedProperties)
189 {
190 }