2 * Copyright (C) 2012, BMW AG
4 * This file is part of GENIVI Project AudioManager.
6 * Contributions are licensed to the GENIVI Alliance under one or more
7 * Contribution License Agreements.
10 * This Source Code Form is subject to the terms of the
11 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
12 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
17 * \file CAmDatabaseHandlerMap.h
18 * For further information see http://www.genivi.org/.
25 #include "IAmDatabaseHandler.h"
30 #include <unordered_map>
36 #ifndef AM_MAP_CAPACITY
37 #define AM_MAP_CAPACITY 0
40 //todo: check the enum values before entering & changing in the database.
41 //todo: change asserts for dynamic boundary checks into failure answers.#
42 //todo: check autoincrement boundary and set to 16bit limits
43 //todo: If the sink is part of a gateway, the listconnectionFormats is copied to the gatewayInformation. Check this statement for sinks & sources
44 //todo: exchange last_insert_row id to be more safe
45 //todo: create test to ensure uniqueness of names throughout the database
46 //todo: enforce the uniqueness of names
49 * This class handles and abstracts the database
51 class CAmDatabaseHandlerMap : public IAmDatabaseHandler
53 bool mFirstStaticSink; //!< bool for dynamic range handling
54 bool mFirstStaticSource; //!< bool for dynamic range handling
55 bool mFirstStaticGateway; //!< bool for dynamic range handling
56 bool mFirstStaticSinkClass; //!< bool for dynamic range handling
57 bool mFirstStaticSourceClass; //!< bool for dynamic range handling
58 bool mFirstStaticCrossfader; //!< bool for dynamic range handling
61 CAmDatabaseHandlerMap();
62 virtual ~CAmDatabaseHandlerMap();
63 am_Error_e enterDomainDB(const am_Domain_s& domainData, am_domainID_t& domainID);
64 am_Error_e enterMainConnectionDB(const am_MainConnection_s& mainConnectionData, am_mainConnectionID_t& connectionID);
65 am_Error_e enterSinkDB(const am_Sink_s& sinkData, am_sinkID_t& sinkID);
66 am_Error_e enterCrossfaderDB(const am_Crossfader_s& crossfaderData, am_crossfaderID_t& crossfaderID);
67 am_Error_e enterGatewayDB(const am_Gateway_s& gatewayData, am_gatewayID_t& gatewayID);
68 am_Error_e enterSourceDB(const am_Source_s& sourceData, am_sourceID_t& sourceID);
69 am_Error_e enterConnectionDB(const am_Connection_s& connection, am_connectionID_t& connectionID);
70 am_Error_e enterSinkClassDB(const am_SinkClass_s& sinkClass, am_sinkClass_t& sinkClassID);
71 am_Error_e enterSourceClassDB(am_sourceClass_t& sourceClassID, const am_SourceClass_s& sourceClass);
72 am_Error_e enterSystemProperties(const std::vector<am_SystemProperty_s>& listSystemProperties);
73 am_Error_e changeMainConnectionRouteDB(const am_mainConnectionID_t mainconnectionID, const std::vector<am_connectionID_t>& listConnectionID);
74 am_Error_e changeMainConnectionStateDB(const am_mainConnectionID_t mainconnectionID, const am_ConnectionState_e connectionState);
75 am_Error_e changeSinkMainVolumeDB(const am_mainVolume_t mainVolume, const am_sinkID_t sinkID);
76 am_Error_e changeSinkAvailabilityDB(const am_Availability_s& availability, const am_sinkID_t sinkID);
77 am_Error_e changDomainStateDB(const am_DomainState_e domainState, const am_domainID_t domainID);
78 am_Error_e changeSinkMuteStateDB(const am_MuteState_e muteState, const am_sinkID_t sinkID);
79 am_Error_e changeMainSinkSoundPropertyDB(const am_MainSoundProperty_s& soundProperty, const am_sinkID_t sinkID);
80 am_Error_e changeMainSourceSoundPropertyDB(const am_MainSoundProperty_s& soundProperty, const am_sourceID_t sourceID);
81 am_Error_e changeSourceSoundPropertyDB(const am_SoundProperty_s& soundProperty, const am_sourceID_t sourceID);
82 am_Error_e changeSinkSoundPropertyDB(const am_SoundProperty_s& soundProperty, const am_sinkID_t sinkID);
83 am_Error_e changeSourceAvailabilityDB(const am_Availability_s& availability, const am_sourceID_t sourceID);
84 am_Error_e changeSystemPropertyDB(const am_SystemProperty_s& property);
85 am_Error_e changeDelayMainConnection(const am_timeSync_t & delay, const am_mainConnectionID_t & connectionID);
86 am_Error_e changeSinkClassInfoDB(const am_SinkClass_s& sinkClass);
87 am_Error_e changeSourceClassInfoDB(const am_SourceClass_s& sourceClass);
88 am_Error_e changeConnectionTimingInformation(const am_connectionID_t connectionID, const am_timeSync_t delay);
89 am_Error_e changeConnectionFinal(const am_connectionID_t connectionID);
90 am_Error_e changeSourceState(const am_sourceID_t sourceID, const am_SourceState_e sourceState);
91 am_Error_e changeSinkVolume(const am_sinkID_t sinkID, const am_volume_t volume);
92 am_Error_e changeSourceVolume(const am_sourceID_t sourceID, const am_volume_t volume);
93 am_Error_e changeCrossFaderHotSink(const am_crossfaderID_t crossfaderID, const am_HotSink_e hotsink);
94 am_Error_e removeMainConnectionDB(const am_mainConnectionID_t mainConnectionID);
95 am_Error_e removeSinkDB(const am_sinkID_t sinkID);
96 am_Error_e removeSourceDB(const am_sourceID_t sourceID);
97 am_Error_e removeGatewayDB(const am_gatewayID_t gatewayID);
98 am_Error_e removeCrossfaderDB(const am_crossfaderID_t crossfaderID);
99 am_Error_e removeDomainDB(const am_domainID_t domainID);
100 am_Error_e removeSinkClassDB(const am_sinkClass_t sinkClassID);
101 am_Error_e removeSourceClassDB(const am_sourceClass_t sourceClassID);
102 am_Error_e removeConnection(const am_connectionID_t connectionID);
103 am_Error_e getSourceClassInfoDB(const am_sourceID_t sourceID, am_SourceClass_s& classInfo) const;
104 am_Error_e getSinkClassInfoDB(const am_sinkID_t sinkID, am_SinkClass_s& sinkClass) const;
105 am_Error_e getGatewayInfoDB(const am_gatewayID_t gatewayID, am_Gateway_s& gatewayData) const;
106 am_Error_e getSinkInfoDB(const am_sinkID_t sinkID, am_Sink_s& sinkData) const;
107 am_Error_e getSourceInfoDB(const am_sourceID_t sourceID, am_Source_s& sourceData) const;
108 am_Error_e getCrossfaderInfoDB(const am_crossfaderID_t crossfaderID, am_Crossfader_s& crossfaderData) const;
109 am_Error_e getMainConnectionInfoDB(const am_mainConnectionID_t mainConnectionID, am_MainConnection_s& mainConnectionData) const;
110 am_Error_e getSinkVolume(const am_sinkID_t sinkID, am_volume_t& volume) const;
111 am_Error_e getSourceVolume(const am_sourceID_t sourceID, am_volume_t& volume) const;
112 am_Error_e getSinkSoundPropertyValue(const am_sinkID_t sinkID, const am_SoundPropertyType_e propertyType, int16_t& value) const;
113 am_Error_e getSourceSoundPropertyValue(const am_sourceID_t sourceID, const am_SoundPropertyType_e propertyType, int16_t& value) const;
114 am_Error_e getListSinksOfDomain(const am_domainID_t domainID, std::vector<am_sinkID_t>& listSinkID) const;
115 am_Error_e getListSourcesOfDomain(const am_domainID_t domainID, std::vector<am_sourceID_t>& listSourceID) const;
116 am_Error_e getListCrossfadersOfDomain(const am_domainID_t domainID, std::vector<am_crossfaderID_t>& listGatewaysID) const;
117 am_Error_e getListGatewaysOfDomain(const am_domainID_t domainID, std::vector<am_gatewayID_t>& listGatewaysID) const;
118 am_Error_e getListMainConnections(std::vector<am_MainConnection_s>& listMainConnections) const;
119 am_Error_e getListDomains(std::vector<am_Domain_s>& listDomains) const;
120 am_Error_e getListConnections(std::vector<am_Connection_s>& listConnections) const;
121 am_Error_e getListSinks(std::vector<am_Sink_s>& listSinks) const;
122 am_Error_e getListSources(std::vector<am_Source_s>& lisSources) const;
123 am_Error_e getListSourceClasses(std::vector<am_SourceClass_s>& listSourceClasses) const;
124 am_Error_e getListCrossfaders(std::vector<am_Crossfader_s>& listCrossfaders) const;
125 am_Error_e getListGateways(std::vector<am_Gateway_s>& listGateways) const;
126 am_Error_e getListSinkClasses(std::vector<am_SinkClass_s>& listSinkClasses) const;
127 am_Error_e getListVisibleMainConnections(std::vector<am_MainConnectionType_s>& listConnections) const;
128 am_Error_e getListMainSinks(std::vector<am_SinkType_s>& listMainSinks) const;
129 am_Error_e getListMainSources(std::vector<am_SourceType_s>& listMainSources) const;
130 am_Error_e getListMainSinkSoundProperties(const am_sinkID_t sinkID, std::vector<am_MainSoundProperty_s>& listSoundProperties) const;
131 am_Error_e getListMainSourceSoundProperties(const am_sourceID_t sourceID, std::vector<am_MainSoundProperty_s>& listSourceProperties) const;
132 am_Error_e getListSystemProperties(std::vector<am_SystemProperty_s>& listSystemProperties) const;
133 am_Error_e getListSinkConnectionFormats(const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const;
134 am_Error_e getListSourceConnectionFormats(const am_sourceID_t sourceID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const;
135 am_Error_e getListGatewayConnectionFormats(const am_gatewayID_t gatewayID, std::vector<bool> & listConnectionFormat) const;
136 am_Error_e getTimingInformation(const am_mainConnectionID_t mainConnectionID, am_timeSync_t& delay) const;
137 am_Error_e getDomainOfSource(const am_sourceID_t sourceID, am_domainID_t& domainID) const;
138 am_Error_e getDomainOfSink(const am_sinkID_t sinkID, am_domainID_t& domainID) const;
139 am_Error_e getSoureState(const am_sourceID_t sourceID, am_SourceState_e& sourceState) const;
140 am_Error_e getDomainState(const am_domainID_t domainID, am_DomainState_e& state) const;
141 am_Error_e getRoutingTree(bool onlyfree, CAmRoutingTree& tree, std::vector<CAmRoutingTreeItem*>& flatTree);
142 am_Error_e peekDomain(const std::string& name, am_domainID_t& domainID);
143 am_Error_e peekSink(const std::string& name, am_sinkID_t& sinkID);
144 am_Error_e peekSource(const std::string& name, am_sourceID_t& sourceID);
145 am_Error_e peekSinkClassID(const std::string& name, am_sinkClass_t& sinkClassID);
146 am_Error_e peekSourceClassID(const std::string& name, am_sourceClass_t& sourceClassID);
147 am_Error_e changeSourceDB(const am_sourceID_t sourceID, const am_sourceClass_t sourceClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_ConnectionFormat_e>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties);
148 am_Error_e changeSinkDB(const am_sinkID_t sinkID, const am_sinkClass_t sinkClassID, const std::vector<am_SoundProperty_s>& listSoundProperties, const std::vector<am_ConnectionFormat_e>& listConnectionFormats, const std::vector<am_MainSoundProperty_s>& listMainSoundProperties);
149 am_Error_e getListMainSinkNotificationConfigurations(const am_sinkID_t sinkID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations);
150 am_Error_e getListMainSourceNotificationConfigurations(const am_sourceID_t sourceID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations);
151 am_Error_e changeMainSinkNotificationConfigurationDB(const am_sinkID_t sinkID, const am_NotificationConfiguration_s mainNotificationConfiguration);
152 am_Error_e changeMainSourceNotificationConfigurationDB(const am_sourceID_t sourceID, const am_NotificationConfiguration_s mainNotificationConfiguration);
153 am_Error_e changeGatewayDB(const am_gatewayID_t gatewayID, const std::vector<am_ConnectionFormat_e>& listSourceConnectionFormats, const std::vector<am_ConnectionFormat_e>& listSinkConnectionFormats, const std::vector<bool>& convertionMatrix);
154 am_Error_e changeSinkNotificationConfigurationDB(const am_sinkID_t sinkID,const am_NotificationConfiguration_s notificationConfiguration);
155 am_Error_e changeSourceNotificationConfigurationDB(const am_sourceID_t sourceID,const am_NotificationConfiguration_s notificationConfiguration);
157 bool existMainConnection(const am_mainConnectionID_t mainConnectionID) const;
158 bool existcrossFader(const am_crossfaderID_t crossfaderID) const;
159 bool existConnection(const am_Connection_s & connection) const;
160 bool existConnectionID(const am_connectionID_t connectionID) const;
161 bool existSource(const am_sourceID_t sourceID) const;
162 bool existSourceNameOrID(const am_sourceID_t sourceID, const std::string& name) const;
163 bool existSourceName(const std::string& name) const;
164 bool existSink(const am_sinkID_t sinkID) const;
165 bool existSinkNameOrID(const am_sinkID_t sinkID, const std::string& name) const;
166 bool existSinkName(const std::string& name) const;
167 bool existDomain(const am_domainID_t domainID) const;
168 bool existGateway(const am_gatewayID_t gatewayID) const;
169 bool existSinkClass(const am_sinkClass_t sinkClassID) const;
170 bool existSourceClass(const am_sourceClass_t sourceClassID) const;
171 void registerObserver(CAmDatabaseObserver *iObserver);
172 bool sourceVisible(const am_sourceID_t sourceID) const;
173 bool sinkVisible(const am_sinkID_t sinkID) const;
175 void dump( std::ostream & output ) const;
177 * The following structures extend the base structures with the field 'reserved'.
180 #define AM_TYPEDEF_SUBCLASS_BEGIN(Subclass, Class) \
181 typedef struct Subclass : public Class\
183 Subclass & operator=(const Subclass & anObject) \
185 if (this != &anObject)\
187 Class::operator=(anObject);\
191 Subclass & operator=(const Class & anObject)\
193 if (this != &anObject)\
194 Class::operator=(anObject);\
197 void getDescription (std::string & outString) const;\
199 #define AM_TYPEDEF_SUBCLASS_RESERVED_FLAG_BEGIN(Subclass, Class) \
200 typedef struct Subclass : public Class\
203 Subclass():Class(), reserved(false)\
205 Subclass & operator=(const Subclass & anObject)\
207 if (this != &anObject)\
209 Class::operator=(anObject);\
210 reserved = anObject.reserved;\
214 Subclass & operator=(const Class & anObject)\
216 if (this != &anObject)\
217 Class::operator=(anObject);\
220 void getDescription (std::string & outString) const;\
222 #define AM_TYPEDEF_SUBCLASS_END(Typedef) } Typedef; \
224 AM_TYPEDEF_SUBCLASS_RESERVED_FLAG_BEGIN(am_Domain_Database_s,am_Domain_s)
225 AM_TYPEDEF_SUBCLASS_END(CAmDomain)
227 AM_TYPEDEF_SUBCLASS_RESERVED_FLAG_BEGIN(am_Sink_Database_s,am_Sink_s)
228 void getSinkType(am_SinkType_s & sinkType) const;\
229 AM_TYPEDEF_SUBCLASS_END(CAmSink)
231 AM_TYPEDEF_SUBCLASS_RESERVED_FLAG_BEGIN(am_Source_Database_s,am_Source_s)
232 void getSourceType(am_SourceType_s & sourceType) const;\
233 AM_TYPEDEF_SUBCLASS_END(CAmSource)
235 AM_TYPEDEF_SUBCLASS_RESERVED_FLAG_BEGIN(am_Connection_Database_s,am_Connection_s)
236 AM_TYPEDEF_SUBCLASS_END(CAmConnection)
239 * The following structures extend the base structures with print capabilities.
241 AM_TYPEDEF_SUBCLASS_BEGIN(am_MainConnection_Database_s, am_MainConnection_s)
242 void getMainConnectionType(am_MainConnectionType_s & connectionType) const;\
243 AM_TYPEDEF_SUBCLASS_END(CAmMainConnection)
245 AM_TYPEDEF_SUBCLASS_BEGIN(am_SourceClass_Database_s, am_SourceClass_s)
246 AM_TYPEDEF_SUBCLASS_END(CAmSourceClass)
248 AM_TYPEDEF_SUBCLASS_BEGIN(am_SinkClass_Database_s, am_SinkClass_s)
249 AM_TYPEDEF_SUBCLASS_END(CAmSinkClass)
251 AM_TYPEDEF_SUBCLASS_BEGIN(am_Gateway_Database_s, am_Gateway_s)
252 AM_TYPEDEF_SUBCLASS_END(CAmGateway)
254 AM_TYPEDEF_SUBCLASS_BEGIN(am_Crossfader_Database_s, am_Crossfader_s)
255 AM_TYPEDEF_SUBCLASS_END(CAmCrossfader)
258 typedef std::unordered_map<am_domainID_t, CAmDomain> CAmMapDomain;
259 typedef std::unordered_map<am_sourceClass_t, CAmSourceClass> CAmMapSourceClass;
260 typedef std::unordered_map<am_sinkClass_t, CAmSinkClass> CAmMapSinkClass;
261 typedef std::unordered_map<am_sinkID_t, CAmSink> CAmMapSink;
262 typedef std::unordered_map<am_sourceID_t, CAmSource> CAmMapSource;
263 typedef std::unordered_map<am_gatewayID_t, CAmGateway> CAmMapGateway;
264 typedef std::unordered_map<am_crossfaderID_t, CAmCrossfader> CAmMapCrossfader;
265 typedef std::unordered_map<am_connectionID_t, CAmConnection> CAmMapConnection;
266 typedef std::unordered_map<am_mainConnectionID_t, CAmMainConnection> CAmMapMainConnection;
267 typedef std::vector<am_SystemProperty_s> CAmVectorSystemProperties;
269 * The following structure groups the map objects needed for the implementation.
270 * Every map object is coupled with an identifier, which hold the current value.
271 * DYNAMIC_ID_BOUNDARY is used as initial value everywhere a dynamic id is considered .
272 * The IDs can be increased through the method increaseID(...), which follows the AudioManager logic.
273 * For more information about the static and dynamic IDs, please see the documentation.
275 typedef struct CAmMappedData
277 int16_t mCurrentDomainID; //!< domain ID
278 int16_t mCurrentSourceClassesID; //!< source classes ID
279 int16_t mCurrentSinkClassesID; //!< sink classes ID
280 int16_t mCurrentSinkID; //!< sink ID
281 int16_t mCurrentSourceID; //!< source ID
282 int16_t mCurrentGatewayID; //!< gateway ID
283 int16_t mCurrentCrossfaderID; //!< crossfader ID
284 int16_t mCurrentConnectionID; //!< connection ID
285 int16_t mCurrentMainConnectionID; //!< mainconnection ID
287 int16_t mDefaultIDLimit; //!< Upper limit for all IDs, default is SHRT_MAX
289 CAmVectorSystemProperties mSystemProperties; //!< vector with system properties
290 CAmMapDomain mDomainMap; //!< map for domain structures
291 CAmMapSourceClass mSourceClassesMap; //!< map for source classes structures
292 CAmMapSinkClass mSinkClassesMap; //!< map for sink classes structures
293 CAmMapSink mSinkMap; //!< map for sink structures
294 CAmMapSource mSourceMap; //!< map for source structures
295 CAmMapGateway mGatewayMap; //!< map for gateway structures
296 CAmMapCrossfader mCrossfaderMap; //!< map for crossfader structures
297 CAmMapConnection mConnectionMap; //!< map for connection structures
298 CAmMapMainConnection mMainConnectionMap; //!< map for main connection structures
300 CAmMappedData(): //For Domain, MainConnections, Connections we don't have static IDs.
301 mCurrentDomainID(1), mCurrentSourceClassesID(DYNAMIC_ID_BOUNDARY), mCurrentSinkClassesID(DYNAMIC_ID_BOUNDARY),
302 mCurrentSinkID(DYNAMIC_ID_BOUNDARY), mCurrentSourceID(DYNAMIC_ID_BOUNDARY), mCurrentGatewayID(DYNAMIC_ID_BOUNDARY),
303 mCurrentCrossfaderID(DYNAMIC_ID_BOUNDARY), mCurrentConnectionID(1), mCurrentMainConnectionID(1),
304 mDefaultIDLimit(SHRT_MAX),
307 mDomainMap(),mSourceClassesMap(), mSinkClassesMap(), mSinkMap(AM_MAP_CAPACITY), mSourceMap(AM_MAP_CAPACITY),
308 mGatewayMap(), mCrossfaderMap(), mConnectionMap(), mMainConnectionMap()
311 * \brief Increases a given map ID.
313 * A common method implementing the logic for static and dynamic IDs except main connection ID.
315 * @param resultID Pointer to an output variable.
316 * @param sourceID Pointer to ID, which will be manipulated.
317 * @param desiredStaticID Not 0 for static IDs and 0 for dynamic IDs.
318 * Usually the static IDs are in interval [1 , DYNAMIC_ID_BOUNDARY]. Default is 0.
319 * @param preferedStaticIDBoundary A limit for a given dynamic ID. Default is DYNAMIC_ID_BOUNDARY.
320 * @return TRUE on successfully changed ID.
322 bool increaseID(int16_t * resultID, int16_t * sourceID,
323 int16_t const desiredStaticID, int16_t const preferedStaticIDBoundary );
325 * \brief Increases the main connection ID.
327 * @param resultID Pointer to an output variable.
328 * @return TRUE on successfully changed ID.
330 bool increaseMainConnectionID(int16_t * resultID);
332 template <class TPrintObject> static void print (const TPrintObject & t, std::ostream & output)
334 std::string description;
335 t.getDescription( description );
336 output << description;
338 template <typename TPrintMapKey,class TPrintMapObject> static void printMap (const std::unordered_map<TPrintMapKey, TPrintMapObject> & t, std::ostream & output)
340 typename std::unordered_map<TPrintMapKey, TPrintMapObject>::const_iterator iter = t.begin();
341 for(; iter!=t.end(); iter++)
342 CAmMappedData::print(iter->second, output);
348 am_timeSync_t calculateMainConnectionDelay(const am_mainConnectionID_t mainConnectionID) const; //!< calculates a new main connection delay
349 int16_t calculateDelayForRoute(const std::vector<am_connectionID_t>& listConnectionID);
350 bool insertSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID);
351 bool insertCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID);
352 bool insertGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID);
353 bool insertSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID);
354 bool insertSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID);
355 bool insertSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass);
356 const am_Sink_Database_s * sinkWithNameOrID(const am_sinkID_t sinkID, const std::string & name) const;
357 const am_Source_Database_s * sourceWithNameOrID(const am_sourceID_t sourceID, const std::string & name) const;
359 CAmDatabaseObserver *mpDatabaseObserver; //!< pointer to the Observer
360 ListConnectionFormat mListConnectionFormat; //!< list of connection formats
361 CAmMappedData mMappedData; //!< Internal structure encapsulating all the maps used in this class
366 #endif /* MAPHANDLER_H_ */