ambd: remove redundant code in core
[profile/ivi/automotive-message-broker.git] / lib / 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->quickCopy(request.value);
85                 routingEngine->updateProperty(value, uuid());
86                 reply->success = true;
87                 reply->error = AsyncPropertyReply::NoError;
88         }
89
90         try {
91                 if(reply->completed)
92                         reply->completed(reply);
93         }
94         catch (...) { }
95
96         return reply;
97 }
98
99 void AmbPluginImpl::subscribeToPropertyChanges(const VehicleProperty::Property& property)
100 {
101
102 }
103
104 PropertyList AmbPluginImpl::supported() const
105 {
106         PropertyList props;
107         for(auto itPropMap = properties.begin(); itPropMap != properties.end(); ++itPropMap)
108                 props.push_back(itPropMap->first);
109         return props;
110 }
111
112 int AmbPluginImpl::supportedOperations() const
113 {
114         return AbstractSource::Get | AbstractSource::Set;
115 }
116
117 void AmbPluginImpl::unsubscribeToPropertyChanges(const VehicleProperty::Property& property)
118 {
119 }
120
121 // if signal does not exits return nullptr(we do not know its datatype), if zone does not exists, add it
122 AbstractPropertyType* AmbPluginImpl::findPropertyType(const VehicleProperty::Property& propertyName, const Zone::Type& zone)
123 {
124         auto itPropMap = properties.find(propertyName);
125         if(itPropMap == properties.end())
126                 return nullptr;
127
128         for( auto it = itPropMap->second.begin(); it != itPropMap->second.end(); ++it ) {
129                 if(it->first == zone)
130                         return it->second.get();
131         }
132
133         return nullptr;
134 }
135
136 std::shared_ptr<AbstractPropertyType> AmbPluginImpl::addPropertySupport(Zone::Type zone, std::function<AbstractPropertyType* (void)> typeFactory, std::string sourceUuid)
137 {
138         if(sourceUuid.empty())
139                 sourceUuid = uuid();
140         std::shared_ptr<AbstractPropertyType> propertyType(typeFactory());
141         if(!propertyType)
142                 return propertyType;
143
144         VehicleProperty::Property name(propertyType->name);
145         PropertyList registeredProperties(VehicleProperty::capabilities());
146         bool registeredType(false);
147         if(!contains(registeredProperties,name))
148         {
149                 registeredType = VehicleProperty::registerProperty(name, typeFactory);
150         }
151         if(!registeredType)
152         { // Property type wasn't registered by us. Is it predefined in AMB API or some other source plug-in has already registered it ???
153                 std::shared_ptr<AbstractPropertyType> registeredPropertyType(VehicleProperty::getPropertyTypeForPropertyNameValue(name));
154                 if(!registeredPropertyType)
155                         return nullptr;
156                 propertyType.swap(registeredPropertyType);
157         }
158         propertyType->zone = zone;
159         propertyType->sourceUuid = sourceUuid;
160         propertyType->timestamp = amb::currentTime();
161         ZonePropertyType& zonePropType = properties[name];
162         zonePropType.insert(make_pair(zone, propertyType));
163         return propertyType;
164 }
165
166 PropertyInfo AmbPluginImpl::getPropertyInfo(const VehicleProperty::Property & property)
167 {
168         auto it = properties.find(property);
169         if(it != properties.end()) {
170                 Zone::ZoneList zones;
171                 for(auto itZonePropType = it->second.begin(); itZonePropType != it->second.end(); ++itZonePropType)
172                         zones.push_back(itZonePropType->first);
173
174                 return PropertyInfo( 0, zones );
175         }
176
177         return PropertyInfo::invalid();
178 }
179
180 void AmbPluginImpl::propertyChanged(AbstractPropertyType* value)
181 {
182 }
183
184 void AmbPluginImpl::supportedChanged(const PropertyList& supportedProperties)
185 {
186 }