Merge pull request #67 from tripzero/trip
[profile/ivi/automotive-message-broker.git] / lib / ambpluginimpl.h
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 #ifndef _AMBPLUGINIMPL_H_
20 #define _AMBPLUGINIMPL_H_
21
22 #include "abstractsource.h"
23
24 /*! \addtogroup libamb
25  *  @{
26  */
27
28 /*!
29  * \brief AmbPlugin private class implementation - base class for all plugin implementations.
30  *
31  * Contains common code used in plugins for Automotive message broker(AMB).
32  * For the AMB library API please visit \ref libamb.
33  *
34  * \class AmbPluginImpl
35  */
36 class AmbPluginImpl
37 {
38
39 public:
40         /*!
41          * \param re AbstractRoutingEngine
42          * \param config Map of the configuration string values loaded on startup from AMB configuration file
43          * \param parent AmbPlugin instance
44          */
45         AmbPluginImpl(AbstractRoutingEngine* re, const map<string, string>& config, AbstractSource &parent);
46         virtual ~AmbPluginImpl() { } /*LCOV_EXCL_LINE*/
47
48         //  aka AbstractSource:
49 public:
50
51         /*!
52          * \brief getPropertyAsync is called when a sink requests the value for given property.
53          * This is only called if the source supports the Get operation.
54          * \param reply the reply variable.
55          */
56         virtual void getPropertyAsync(AsyncPropertyReply *reply);
57
58         /*!
59          * \brief getRangePropertyAsync is called when a sink requests a series of values for a given
60          * property within a specified time or sequencial range.  This will only be called if the source
61          * support the Ranged Operation.
62          * \param reply is the reply variable.
63          */
64         virtual void getRangePropertyAsync(AsyncRangePropertyReply *reply);
65
66         /*!
67          * \brief setProperty is called when a sink requests to set a value for a given property.
68          * This is only called if the source supports the Set Operation.
69          * \param request the requested property to set.
70          * \return returns a pointer to the new value for the property.
71          */
72         virtual AsyncPropertyReply *setProperty(const AsyncSetPropertyRequest& request );
73
74         /*!
75          * \brief subscribeToPropertyChanges is called when a sink requests a subscription.  Source plugins
76          * can keep track of subscriptions and may wish to sleep if there are no subscriptions.
77          * \param property the property that is being subscribed.
78          */
79         virtual void subscribeToPropertyChanges(const VehicleProperty::Property& property);
80
81         /*!
82          * \brief unsubscribeToPropertyChanges is called when a sink requests to unsubscribe from a given property's changes.
83          * \param property the property to unsubscribe to
84          */
85         virtual void unsubscribeToPropertyChanges(const VehicleProperty::Property& property);
86
87         /*!
88          * \brief supported is called by the routingEngine to understand what properties this source supports.
89          * \return returns a list of supported properties.  If the the supported properties changed, the source should call AbstractRoutingEngine::setSupported.
90          */
91         virtual PropertyList supported() const;
92
93         /*!
94          * \brief supportedOperations
95          * \return returns the supported operations.
96          */
97         virtual int supportedOperations() const;
98
99         /*!
100          * \brief getPropertyInfo used to return specific information about a property.
101          * The source should override this otherwise a PropertyInfo::invalid() will be returned for the property
102          * \param property the property to get info for.
103          * \return a PropertyInfo object.
104          */
105         virtual PropertyInfo getPropertyInfo(const VehicleProperty::Property & property);
106
107         // aka AbstractSink:
108 public:
109
110         /*! uuid() is a unique identifier
111           * @return a guid-style unique identifier
112           */
113         virtual const std::string uuid() const = 0;
114
115         /*! propertyChanged is called when a subscribed to property changes.
116           * @param value value of the property that changed. this is a temporary pointer that will be destroyed.
117           * Do not destroy it.  If you need to store the value use value.anyValue(), value.value<T>() or
118           * value->copy() to copy.
119           */
120         virtual void propertyChanged(AbstractPropertyType* value);
121
122         /*! supportedChanged() is called when the supported properties changes
123          * \fn supportedChanged
124          * \param supportedProperties the new list of supported properties.
125          */
126         virtual void supportedChanged(const PropertyList& supportedProperties);
127
128         /*!
129          * Second phase of the plugin initialization.
130          * \fn init
131          */
132         virtual void init();
133
134         /*!
135          * \brief setValue of a property
136          */
137         template <typename T>
138         void setValue(std::shared_ptr<AbstractPropertyType> property, T value)
139         {
140                 if(property->value<T>() == value)
141                 {
142                         return;
143                 }
144
145                 property->setValue(value);
146                 routingEngine->updateProperty(property.get(), source.uuid());
147         }
148
149
150 protected:
151
152         /*! Finds property type in #properties
153          * \param propertyName Name of the property to be found.
154          * \param zone Zone of the property to be found.
155          * \return AbstractPropertyType* if signal exits otherwise nullptr(in this case we do not know its datatype)
156          */
157         virtual AbstractPropertyType* findPropertyType(const VehicleProperty::Property& propertyName, const Zone::Type& zone = Zone::None);
158
159         /*! Registers property in AMB
160          * \param zone Zone of the property to be registered.
161          * \param typeFactory Function to be used to create instance of the AbstractPropertyType for registered property
162          * \return AbstractPropertyType* if signal exits otherwise nullptr(in this case we do not know its datatype)
163          */
164         std::shared_ptr<AbstractPropertyType> addPropertySupport(Zone::Type zone, std::function<AbstractPropertyType* (void)> typeFactory, std::string sourceUuid="");
165
166         template <class T>
167         std::shared_ptr<AbstractPropertyType> addPropertySupport(Zone::Type zone)
168         {
169                 auto typeFactory = [](){
170                         return new T;
171                 };
172                 return addPropertySupport(zone, typeFactory);
173         }
174
175         //
176         // data:
177         //
178
179         /*! AmbPlugin instance
180          * \property parent
181          */
182         AbstractSource& source;
183
184         /*!
185          * AbstractRoutingEngine instance
186          * \property routingEngine
187          */
188         AbstractRoutingEngine* routingEngine;
189
190         /*! Helper typedef
191          * \internal
192          */
193         typedef std::map< Zone::Type, std::shared_ptr<AbstractPropertyType> > ZonePropertyType;
194
195         /*!
196          * Supported property values map
197          * \property properties
198          */
199         std::map< VehicleProperty::Property, ZonePropertyType > properties;
200
201         /*!
202           * configuration
203           */
204         std::map< std::string, std::string> configuration;
205 };
206
207 #endif // _AMBPLUGINIMPL_H_
208
209 /** @} */