505aa964a97cb9158e452d4666bf598081644e85
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / src / CAmDatabaseHandlerMap.cpp
1 /**
2  * Copyright (C) 2012, BMW AG
3  *
4  * This file is part of GENIVI Project AudioManager.
5  *
6  * Contributions are licensed to the GENIVI Alliance under one or more
7  * Contribution License Agreements.
8  *
9  * \copyright
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/.
13  *
14  *
15  * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
16  *
17  * \file CAmDatabaseHandlerMap.cpp
18  * For further information see http://www.genivi.org/.
19  *
20  */
21
22 #include "CAmDatabaseHandlerMap.h"
23 #include <iostream>
24 #include <cassert>
25 #include <stdexcept>
26 #include <vector>
27 #include <fstream>
28 #include <sstream>
29 #include <string>
30 #include <algorithm>
31 #include <limits>
32 #include "CAmDatabaseObserver.h"
33 #include "CAmRouter.h"
34 #include "shared/CAmDltWrapper.h"
35 #include "CAmLog.h"
36
37
38 namespace am
39 {
40
41 /*
42  * Returns an object for given key
43  */
44 template <typename TMapKeyType, class TMapObjectType> TMapObjectType const * objectForKeyIfExistsInMap(const TMapKeyType & key, const std::unordered_map<TMapKeyType,TMapObjectType> & map)
45 {
46         typename std::unordered_map<TMapKeyType,TMapObjectType>::const_iterator iter = map.find(key);
47         if( iter!=map.end() )
48                 return &iter->second;
49         return NULL;
50 }
51
52 /*
53  * Checks whether any object with key exists in a given map
54  */
55 template <typename TMapKeyType, class TMapObjectType> bool existsObjectWithKeyInMap(const TMapKeyType & key, const std::unordered_map<TMapKeyType,TMapObjectType> & map)
56 {
57         return objectForKeyIfExistsInMap(key, map)!=NULL;
58 }
59
60 /*
61  * Comparators
62  */
63 template <class TObject>
64 struct CAmComparator
65 {
66     bool operator()(const TObject & anObject, const std::string & aValue, void *)
67     {
68         return aValue.compare(anObject.name)==0;
69     }
70 };
71
72 struct CAmComparatorFlag
73 {
74     bool operator()(const CAmDatabaseHandlerMap::am_Sink_Database_s & anObject, const std::string & aValue, void *contextOrNull)
75     {
76                 bool flag = (contextOrNull!=NULL)?*((bool*)contextOrNull):true;
77                 return flag==anObject.reserved &&
78                                 aValue.compare(anObject.name)==0;
79
80     }
81     bool operator()(const CAmDatabaseHandlerMap::am_Connection_Database_s &anObject, const am_Connection_s &aValue, void *)
82     {
83                 return 0==anObject.reserved &&
84                                 aValue.sinkID == anObject.sinkID &&
85                                 aValue.sourceID == anObject.sourceID &&
86                                 aValue.connectionFormat == anObject.connectionFormat;
87     }
88     bool operator()(const CAmDatabaseHandlerMap::am_Source_Database_s & anObject, const std::string & aValue, void *contextOrNull)
89     {
90                 bool flag = (contextOrNull!=NULL)?*((bool*)contextOrNull):true;
91                 return flag==anObject.reserved &&
92                                 aValue.compare(anObject.name)==0;
93
94     }
95 };
96
97 /**
98  * \brief Returns an object matching predicate.
99  *
100  * Convenient method for searching in a given map.
101  *
102  * @param map Map reference.
103  * @param comparator Search predicate.
104  * @return NULL or pointer to the found object.
105  */
106 template <class TReturn, typename TIdentifier> const TReturn *  objectMatchingPredicate(const std::unordered_map<TIdentifier, TReturn> & map,
107                                                                                                                                                                                                   std::function<bool(const TReturn & refObject)> & comparator)
108 {
109         typename std::unordered_map<TIdentifier, TReturn>::const_iterator elementIterator = map.begin();
110         for (;elementIterator != map.end(); ++elementIterator)
111         {
112                 if( comparator(elementIterator->second) )
113                         return &elementIterator->second;
114         }
115     return NULL;
116 }
117
118 /**
119  * \brief Returns the first object matching criteria.
120  *
121  * A common method for searching in a given map.
122  *
123  * @param aMap A map reference.
124  * @param aComparisonArgument An object which will be used in the comparison.
125  * @param aComparator A structure which defines comparison operations.
126  * @param context An optional user info passed to the comparators.
127  * @return NULL or pointer to the found object.
128  */
129 template <typename TMapKeyType, class TMapObjectType, class TSearchObjectType, class TComparator>
130 TMapObjectType const * findFirstObjectMatchingCriteria(const std::unordered_map<TMapKeyType, TMapObjectType> & aMap,
131                                                                                                                           const TSearchObjectType & aComparisonArgument,
132                                                                                                                           TComparator & aComparator,
133                                                                                                                           void *context = NULL)
134 {
135         std::function<bool(const TMapObjectType & refObject)> comparator = [&](const TMapObjectType & object)->bool{
136                 return aComparator(object, aComparisonArgument, context);
137         };
138         return objectMatchingPredicate(aMap, comparator);
139 }
140
141 /* Domain */
142
143 void CAmDatabaseHandlerMap::CAmDomain::getDescription (std::string & outString) const
144 {
145         std::ostringstream fmt;
146         fmt << "Domain(" << name.c_str() << ") id(" << domainID << ")" << std::endl <<
147                         "bus name(" << busname.c_str() <<
148                         ") node name(" << nodename.c_str() <<
149                         ") early(" << early <<
150                         ") domainID(" << domainID <<
151                         ") complete(" << complete <<
152                         ") state(" << state <<
153                         ") reserved(" << reserved << ")" << std::endl;
154         outString = fmt.str();
155 }
156
157 /* Source */
158
159 void CAmDatabaseHandlerMap::CAmSource::getSourceType(am_SourceType_s & sourceType) const
160 {
161         sourceType.name = name;
162         sourceType.sourceClassID = sourceClassID;
163         sourceType.availability = available;
164         sourceType.sourceID = sourceID;
165 }
166
167 void CAmDatabaseHandlerMap::CAmSource::getDescription (std::string & outString) const
168 {
169         std::ostringstream fmt;
170         fmt << "Source(" << name.c_str() << ") id(" << sourceID << ")" << std::endl <<
171                         "sourceClassID(" << sourceClassID <<
172                         ") domainID(" << domainID <<
173                         ") visible(" << visible <<
174                         ") volume(" << volume <<
175                         ") interruptState(" << interruptState <<
176                         ") sourceState(" << sourceState <<
177                         ") reserved(" << reserved << ")" <<
178                         ") available([availability:" << available.availability << " availabilityReason:" << available.availabilityReason << "]"
179                         ") listSoundProperties (";
180                         std::for_each(listSoundProperties.begin(), listSoundProperties.end(), [&](const am_SoundProperty_s & ref) {
181                                 fmt << "[type:" << ref.type << " value:" << ref.value <<"]";
182                         });
183                         fmt << ") listConnectionFormats (";
184                         std::for_each(listConnectionFormats.begin(), listConnectionFormats.end(), [&](const am_ConnectionFormat_e & ref) {
185                                 fmt << "[" << ref << "]";
186                         });
187                         fmt << ") listMainSoundProperties (";
188                         std::for_each(listMainSoundProperties.begin(), listMainSoundProperties.end(), [&](const am_MainSoundProperty_s & ref) {
189                                 fmt << "[type:" << ref.type << " value:" << ref.value <<"]";
190                         });
191                         fmt << ") listMainNotificationConfigurations (";
192                         std::for_each(listMainNotificationConfigurations.begin(), listMainNotificationConfigurations.end(), [&](const am_NotificationConfiguration_s & ref) {
193                                 fmt << "[type:" << ref.type << " status:" << ref.status << " parameter:" << ref.parameter <<"]";
194                         });
195                         fmt << ") listNotificationConfigurations (";
196                         std::for_each(listNotificationConfigurations.begin(), listNotificationConfigurations.end(), [&](const am_NotificationConfiguration_s & ref) {
197                                 fmt << "[type:" << ref.type << " status:" << ref.status << " parameter:" << ref.parameter <<"]";
198                         });
199                         fmt <<  ")" << std::endl;
200         outString = fmt.str();
201 }
202
203 /* Sink */
204
205 void CAmDatabaseHandlerMap::CAmSink::getDescription (std::string & outString) const
206 {
207         std::ostringstream fmt;
208         fmt << "Sink(" << name.c_str() << ") id(" << sinkID << ")" << std::endl <<
209                         "sinkClassID(" << sinkClassID <<
210                         ") domainID(" << domainID <<
211                         ") visible(" << visible <<
212                         ") volume(" << volume <<
213                         ") muteState(" << muteState <<
214                         ") mainVolume(" << mainVolume <<
215                         ") reserved(" << reserved << ")" <<
216                         ") available([availability:" << available.availability << " availabilityReason:" << available.availabilityReason << "]"
217                         ") listSoundProperties (";
218                         std::for_each(listSoundProperties.begin(), listSoundProperties.end(), [&](const am_SoundProperty_s & ref) {
219                                 fmt << "[type:" << ref.type << " value:" << ref.value <<"]";
220                         });
221                         fmt << ") listConnectionFormats (";
222                         std::for_each(listConnectionFormats.begin(), listConnectionFormats.end(), [&](const am_ConnectionFormat_e & ref) {
223                                 fmt << "[" << ref << "]";
224                         });
225                         fmt << ") listMainSoundProperties (";
226                         std::for_each(listMainSoundProperties.begin(), listMainSoundProperties.end(), [&](const am_MainSoundProperty_s & ref) {
227                                 fmt << "[type:" << ref.type << " value:" << ref.value <<"]";
228                         });
229                         fmt << ") listMainNotificationConfigurations (";
230                         std::for_each(listMainNotificationConfigurations.begin(), listMainNotificationConfigurations.end(), [&](const am_NotificationConfiguration_s & ref) {
231                                 fmt << "[type:" << ref.type << " status:" << ref.status << " parameter:" << ref.parameter <<"]";
232                         });
233                         fmt << ") listNotificationConfigurations (";
234                         std::for_each(listNotificationConfigurations.begin(), listNotificationConfigurations.end(), [&](const am_NotificationConfiguration_s & ref) {
235                                 fmt << "[type:" << ref.type << " status:" << ref.status << " parameter:" << ref.parameter <<"]";
236                         });
237                         fmt <<  ")" << std::endl;
238         outString = fmt.str();
239 }
240
241 void CAmDatabaseHandlerMap::CAmSink::getSinkType(am_SinkType_s & sinkType) const
242 {
243         sinkType.name = name;
244         sinkType.sinkID = sinkID;
245         sinkType.availability = available;
246         sinkType.muteState = muteState;
247         sinkType.volume = mainVolume;
248         sinkType.sinkClassID = sinkClassID;
249 }
250
251 /* Connection */
252
253 void CAmDatabaseHandlerMap::CAmConnection::getDescription (std::string & outString) const
254 {
255         std::ostringstream fmt;
256         fmt << "Connection id(" << connectionID << ") " << std::endl <<
257                         "sourceID(" << sourceID <<
258                         ") sinkID(" << sinkID <<
259                         ") delay(" << delay <<
260                         ") connectionFormat(" << connectionFormat <<
261                         ") reserved(" << reserved << ")" << std::endl;
262         outString = fmt.str();
263 }
264
265 /* Main Connection */
266
267 void CAmDatabaseHandlerMap::CAmMainConnection::getDescription (std::string & outString) const
268 {
269         std::ostringstream fmt;
270         fmt << "MainConnection id(" << mainConnectionID << ") " << std::endl <<
271                         "connectionState(" << connectionState <<
272                         ") sinkID(" << sinkID <<
273                         ") sourceID(" << sourceID <<
274                         ") delay(" << delay <<
275                         ") listConnectionID (";
276                         std::for_each(listConnectionID.begin(), listConnectionID.end(), [&](const am_connectionID_t & connID) {
277                                 fmt << "["<< connID << "]";
278                         });
279                         fmt << ")" << std::endl;
280         outString = fmt.str();
281 }
282
283 void CAmDatabaseHandlerMap::am_MainConnection_Database_s::getMainConnectionType(am_MainConnectionType_s & connectionType) const
284 {
285         connectionType.mainConnectionID = mainConnectionID;
286         connectionType.sourceID = sourceID;
287         connectionType.sinkID = sinkID;
288         connectionType.connectionState = connectionState;
289         connectionType.delay = delay;
290 }
291
292 /* Source Class */
293
294 void CAmDatabaseHandlerMap::CAmSourceClass::getDescription (std::string & outString) const
295 {
296         std::ostringstream fmt;
297         fmt << "Source class(" << name.c_str() << ") id(" << sourceClassID << ")\n" <<
298                         ") listClassProperties (";
299                         std::for_each(listClassProperties.begin(), listClassProperties.end(), [&](const am_ClassProperty_s & ref) {
300                                 fmt << "[classProperty:" << ref.classProperty << " value:" << ref.value << "]";
301                         });
302                         fmt << ")" << std::endl;
303         outString = fmt.str();
304 }
305
306 /* Sink Class */
307
308 void CAmDatabaseHandlerMap::CAmSinkClass::getDescription (std::string & outString) const
309 {
310         std::ostringstream fmt;
311         fmt << "Sink class(" << name.c_str() << ") id(" << sinkClassID << ")\n" <<
312                         ") listClassProperties (";
313                         std::for_each(listClassProperties.begin(), listClassProperties.end(), [&](const am_ClassProperty_s & ref) {
314                                 fmt << "[classProperty:" << ref.classProperty << " value:" << ref.value << "]";
315                         });
316                         fmt << ")" << std::endl;
317         outString = fmt.str();
318 }
319
320
321 /* Gateway */
322
323 void CAmDatabaseHandlerMap::CAmGateway::getDescription (std::string & outString) const
324 {
325         std::ostringstream fmt;
326         fmt << "Gateway(" << name.c_str() << ") id(" << gatewayID << ")\n" <<
327                         "sinkID(" << sinkID <<
328                         ") sourceID(" << sourceID <<
329                         ") domainSinkID(" << domainSinkID <<
330                         ") domainSourceID(" << domainSourceID <<
331                         ") controlDomainID(" << controlDomainID <<
332                         ") listSourceFormats (";
333                         std::for_each(listSourceFormats.begin(), listSourceFormats.end(), [&](const am_ConnectionFormat_e & ref) {
334                                 fmt << "[" << ref << "]";
335                         });
336                         fmt << ") listSinkFormats (";
337                         std::for_each(listSinkFormats.begin(), listSinkFormats.end(), [&](const am_ConnectionFormat_e & ref) {
338                                 fmt << "[" << ref << "]";
339                         });
340                         fmt << ") convertionMatrix (";
341                         std::for_each(convertionMatrix.begin(), convertionMatrix.end(), [&](const bool & ref) {
342                                 fmt << "[" << ref << "]";
343                         });
344                         fmt << ")" << std::endl;
345         outString = fmt.str();
346 }
347
348 /* Crossfader */
349
350 void CAmDatabaseHandlerMap::CAmCrossfader::getDescription (std::string & outString) const
351 {
352         std::ostringstream fmt;
353         fmt << "Crossfader(" << name.c_str() << ") id(" << crossfaderID << ")\n" <<
354                         "sinkID_A(" << sinkID_A <<
355                         ") sinkID_B(" << sinkID_B <<
356                         ") sourceID(" << sourceID <<
357                         ") hotSink(" << hotSink <<
358                         ")" << std::endl;
359         outString = fmt.str();
360 }
361
362 bool CAmDatabaseHandlerMap::CAmMappedData::increaseID(int16_t * resultID,
363                                                                                                                          int16_t * sourceID,
364                                                                                                                          int16_t const desiredStaticID = 0,
365                                                                                                                          int16_t const preferedStaticIDBoundary = DYNAMIC_ID_BOUNDARY)
366 {
367         if( desiredStaticID > 0 && desiredStaticID < preferedStaticIDBoundary )
368         {
369                 *resultID = desiredStaticID;
370                 return true;
371         }
372         else if( *sourceID < mDefaultIDLimit ) //The last used value is 'limit' - 1. e.g. SHRT_MAX - 1, SHRT_MAX is reserved.
373         {
374                 *resultID = (*sourceID)++;
375                 return true;
376         }
377         else
378         {
379                 *resultID = -1;
380                 return false;
381         }
382  }
383
384 bool CAmDatabaseHandlerMap::CAmMappedData::increaseMainConnectionID(int16_t * resultID)
385 {
386         am_mainConnectionID_t nextID;
387         am_mainConnectionID_t const lastID = mCurrentMainConnectionID;
388         if( mCurrentMainConnectionID < mDefaultIDLimit )
389                 nextID = mCurrentMainConnectionID++;
390         else
391                 nextID = mCurrentMainConnectionID = 1;
392
393         bool notFreeIDs = false;
394         while( existsObjectWithKeyInMap(nextID, mMainConnectionMap) )
395         {
396                 if( mCurrentMainConnectionID < mDefaultIDLimit )
397                         nextID = mCurrentMainConnectionID++;
398                 else
399                         nextID = mCurrentMainConnectionID = 1;
400
401                 if( mCurrentMainConnectionID == lastID )
402                 {
403                         notFreeIDs = true;
404                         break;
405                 }
406         }
407         if(notFreeIDs)
408         {
409                 *resultID = -1;
410                 return false;
411         }
412         *resultID = nextID;
413         return true;
414 }
415
416
417 CAmDatabaseHandlerMap::CAmDatabaseHandlerMap(): mFirstStaticSink(true), //
418                                                                                                                 mFirstStaticSource(true), //
419                                                                                                                 mFirstStaticGateway(true), //
420                                                                                                                 mFirstStaticSinkClass(true), //
421                                                                                                                 mFirstStaticSourceClass(true), //
422                                                                                                                 mFirstStaticCrossfader(true), //
423                                                                                                                 mpDatabaseObserver(NULL), //
424                                                                                                                 mListConnectionFormat(), //
425                                                                                                                 mMappedData()
426 {
427         logInfo(__PRETTY_FUNCTION__,"Init");
428 }
429
430 CAmDatabaseHandlerMap::~CAmDatabaseHandlerMap()
431 {
432     logInfo(__PRETTY_FUNCTION__,"Destroy");
433     mpDatabaseObserver = NULL;
434 }
435
436 am_Error_e CAmDatabaseHandlerMap::enterDomainDB(const am_Domain_s & domainData, am_domainID_t & domainID)
437 {
438     assert(domainData.domainID==0);
439     assert(!domainData.name.empty());
440     assert(!domainData.busname.empty());
441     assert(domainData.state>=DS_UNKNOWN && domainData.state<=DS_MAX);
442     //first check for a reserved domain
443     CAmComparator<am_Domain_s> comparator;
444     am_Domain_s const *reservedDomain = findFirstObjectMatchingCriteria(mMappedData.mDomainMap, domainData.name, comparator);
445
446     int16_t nextID = 0;
447
448     if( NULL != reservedDomain )
449     {
450         nextID = reservedDomain->domainID;
451         domainID = nextID;
452         mMappedData.mDomainMap[nextID] = domainData;
453         mMappedData.mDomainMap[nextID].domainID = nextID;
454         mMappedData.mDomainMap[nextID].reserved = 0;
455         logInfo("DatabaseHandler::enterDomainDB entered reserved domain with name=", domainData.name, "busname=", domainData.busname, "nodename=", domainData.nodename, "reserved ID:", domainID);
456         if (mpDatabaseObserver)
457             mpDatabaseObserver->newDomain(mMappedData.mDomainMap[nextID]);
458         return (E_OK);
459     }
460     else
461     {
462                 if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentDomainID))
463                 {
464                         domainID = nextID;
465                         mMappedData.mDomainMap[nextID] = domainData;
466                         mMappedData.mDomainMap[nextID].domainID = nextID;
467                         logInfo("DatabaseHandler::enterDomainDB entered new domain with name=", domainData.name, "busname=", domainData.busname, "nodename=", domainData.nodename, "assigned ID:", domainID);
468                         if (mpDatabaseObserver)
469                                 mpDatabaseObserver->newDomain(mMappedData.mDomainMap[nextID]);
470                         return (E_OK);
471                 }
472                 else
473                 {
474                         domainID = 0;
475                         logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
476                         return (E_UNKNOWN);
477                 }
478     }
479 }
480
481 int16_t CAmDatabaseHandlerMap::calculateDelayForRoute(const std::vector<am_connectionID_t>& listConnectionID)
482 {
483         int16_t delay = 0;
484         std::vector<am_connectionID_t>::const_iterator elementIterator = listConnectionID.begin();
485         for (; elementIterator < listConnectionID.end(); ++elementIterator)
486         {
487                 am_connectionID_t key = *elementIterator;
488                 std::unordered_map<am_connectionID_t, am_Connection_Database_s>::const_iterator it = mMappedData.mConnectionMap.find(key);
489                 if (it!=mMappedData.mConnectionMap.end())
490                 {
491                         int16_t temp_delay = it->second.delay;
492                         if (temp_delay != -1 && delay != -1)
493                                 delay += temp_delay;
494                         else
495                                 delay = -1;
496                 }
497         }
498         return delay;
499 }
500
501 am_Error_e CAmDatabaseHandlerMap::enterMainConnectionDB(const am_MainConnection_s & mainConnectionData, am_mainConnectionID_t & connectionID)
502 {
503     assert(mainConnectionData.mainConnectionID==0);
504     assert(mainConnectionData.connectionState>=CS_UNKNOWN && mainConnectionData.connectionState<=CS_MAX);
505     assert(mainConnectionData.sinkID!=0);
506     assert(mainConnectionData.sourceID!=0);
507
508     int16_t delay = 0;
509     int16_t nextID = 0;
510         if(mMappedData.increaseMainConnectionID(&nextID))
511         {
512                 connectionID = nextID;
513                 mMappedData.mMainConnectionMap[nextID] = mainConnectionData;
514                 mMappedData.mMainConnectionMap[nextID].mainConnectionID = nextID;
515         }
516         else
517         {
518                 connectionID = 0;
519                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
520                 return (E_UNKNOWN);
521         }
522
523     //now check the connectionTable for all connections in the route. IF connectionID exist
524      delay = calculateDelayForRoute(mainConnectionData.listConnectionID);
525      mMappedData.mMainConnectionMap[nextID].delay = delay;
526     logInfo("DatabaseHandler::enterMainConnectionDB entered new mainConnection with sourceID", mainConnectionData.sourceID, "sinkID:", mainConnectionData.sinkID, "delay:", delay, "assigned ID:", connectionID);
527
528     if (mpDatabaseObserver)
529     {
530         am_MainConnectionType_s mainConnection;
531         mMappedData.mMainConnectionMap[nextID].getMainConnectionType(mainConnection);
532         mpDatabaseObserver->newMainConnection(mainConnection);
533         mpDatabaseObserver->mainConnectionStateChanged(connectionID, mMappedData.mMainConnectionMap[nextID].connectionState);
534     }
535
536     //finally, we update the delay value for the maintable
537     if (delay == 0)
538         delay = -1;
539     return (changeDelayMainConnection(delay, connectionID));
540 }
541
542 /**
543  * Helper method, that inserts a new struct in the map and copies the given into it.
544  * This method uses the increaseID function to secure the global id is properly increased.
545  **/
546 bool CAmDatabaseHandlerMap::insertSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
547 {
548     int16_t nextID = 0;
549         if(  mMappedData.increaseID(&nextID, &mMappedData.mCurrentSinkID, sinkData.sinkID) )
550         {
551                 sinkID = nextID;
552                 mMappedData.mSinkMap[nextID] = sinkData;
553                 mMappedData.mSinkMap[nextID].sinkID = nextID;
554                 return (true);
555         }
556         else
557         {
558                 sinkID = 0;
559                 logInfo(__PRETTY_FUNCTION__,"Max limit reached!");
560                 return (false);
561         }
562 }
563
564 am_Error_e CAmDatabaseHandlerMap::enterSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
565 {
566     assert(sinkData.sinkID<DYNAMIC_ID_BOUNDARY);
567     assert(sinkData.domainID!=0);
568     assert(!sinkData.name.empty());
569     assert(sinkData.sinkClassID!=0);
570     //todo: need to check if class exists?,
571     assert(!sinkData.listConnectionFormats.empty());
572     assert(sinkData.muteState>=MS_UNKNOWN && sinkData.muteState<=MS_MAX);
573
574     am_sinkID_t temp_SinkID = 0;
575     am_sinkID_t temp_SinkIndex = 0;
576         //if sinkID is zero and the first Static Sink was already entered, the ID is created
577     CAmComparatorFlag comparator;
578     bool checkForFlag = true;
579     am_Sink_s const *reservedDomain = findFirstObjectMatchingCriteria(mMappedData.mSinkMap, sinkData.name, comparator, &checkForFlag);
580     if( NULL!=reservedDomain )
581     {
582         am_sinkID_t oldSinkID = reservedDomain->sinkID;
583         mMappedData.mSinkMap[oldSinkID] = sinkData;
584         mMappedData.mSinkMap[oldSinkID].reserved = 0;
585         temp_SinkID = oldSinkID;
586         temp_SinkIndex = oldSinkID;
587     }
588     else
589     {
590                 bool result;
591                 if ( sinkData.sinkID != 0 || mFirstStaticSink )
592                 {
593                         //check if the ID already exists
594                         if (existSinkNameOrID(sinkData.sinkID, sinkData.name))
595                         {
596                                 sinkID = sinkData.sinkID;
597                                 return (E_ALREADY_EXISTS);
598                         }
599                 }
600                 result = insertSinkDB(sinkData, temp_SinkID);
601                 if( false == result )
602                         return (E_UNKNOWN);
603                 temp_SinkIndex = temp_SinkID;
604     }
605     //if the first static sink is entered, we need to set it onto the boundary
606     if (sinkData.sinkID == 0 && mFirstStaticSink)
607     {
608         mFirstStaticSink = false;
609     }
610     mMappedData.mSinkMap[temp_SinkIndex].sinkID = temp_SinkID;
611     sinkID = temp_SinkID;
612
613     am_Sink_s & sink = mMappedData.mSinkMap[temp_SinkID];
614     logInfo("DatabaseHandler::enterSinkDB entered new sink with name", sink.name, "domainID:", sink.domainID, "classID:", sink.sinkClassID, "volume:", sink.volume, "assigned ID:", sink.sinkID);
615
616     if (mpDatabaseObserver != NULL)
617         sink.sinkID=sinkID;
618         mpDatabaseObserver->newSink(sink);
619     return (E_OK);
620 }
621
622 bool CAmDatabaseHandlerMap::insertCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
623 {
624     int16_t nextID = 0;
625         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentCrossfaderID, crossfaderData.crossfaderID))
626         {
627                 crossfaderID = nextID;
628                 mMappedData.mCrossfaderMap[nextID] = crossfaderData;
629                 mMappedData.mCrossfaderMap[nextID].crossfaderID = nextID;
630                 return (true);
631         }
632         else
633         {
634                 crossfaderID = 0;
635                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
636                 return (false);
637         }
638 }
639
640 am_Error_e CAmDatabaseHandlerMap::enterCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
641 {
642     assert(crossfaderData.crossfaderID<DYNAMIC_ID_BOUNDARY);
643     assert(crossfaderData.hotSink>=HS_UNKNOWN && crossfaderData.hotSink<=HS_MAX);
644     assert(!crossfaderData.name.empty());
645     assert(existSink(crossfaderData.sinkID_A));
646     assert(existSink(crossfaderData.sinkID_B));
647     assert(existSource(crossfaderData.sourceID));
648
649     am_crossfaderID_t temp_CrossfaderID = 0;
650     am_crossfaderID_t temp_CrossfaderIndex = 0;
651
652     bool result;
653     //if gatewayData is zero and the first Static Sink was already entered, the ID is created
654     if (crossfaderData.crossfaderID != 0 || mFirstStaticCrossfader)
655     {
656         //check if the ID already exists
657         if (existcrossFader(crossfaderData.crossfaderID))
658         {
659                 crossfaderID = crossfaderData.crossfaderID;
660             return (E_ALREADY_EXISTS);
661         }
662     }
663     result = insertCrossfaderDB(crossfaderData, temp_CrossfaderID);
664         if( false == result )
665                 return (E_UNKNOWN);
666         temp_CrossfaderIndex = temp_CrossfaderID;
667
668     //if the first static sink is entered, we need to set it onto the boundary
669     if ( 0==crossfaderData.crossfaderID && mFirstStaticCrossfader)
670     {
671         mFirstStaticCrossfader = false;
672     }
673
674    mMappedData.mCrossfaderMap[temp_CrossfaderIndex].crossfaderID = temp_CrossfaderID;
675    crossfaderID = temp_CrossfaderID;
676    logInfo("DatabaseHandler::enterCrossfaderDB entered new crossfader with name=", crossfaderData.name, "sinkA= ", crossfaderData.sinkID_A, "sinkB=", crossfaderData.sinkID_B, "source=", crossfaderData.sourceID, "assigned ID:", crossfaderID);
677
678     if (mpDatabaseObserver)
679         mpDatabaseObserver->newCrossfader(mMappedData.mCrossfaderMap[temp_CrossfaderIndex]);
680     return (E_OK);
681 }
682
683 bool CAmDatabaseHandlerMap::insertGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
684 {
685     int16_t nextID = 0;
686         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentGatewayID, gatewayData.gatewayID))
687         {
688                 gatewayID = nextID;
689                 mMappedData.mGatewayMap[nextID] = gatewayData;
690                 mMappedData.mGatewayMap[nextID].gatewayID = nextID;
691                 return (true);
692         }
693         else
694         {
695                 gatewayID = 0;
696                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
697                 return (false);
698         }
699 }
700
701 am_Error_e CAmDatabaseHandlerMap::enterGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
702 {
703     assert(gatewayData.gatewayID<DYNAMIC_ID_BOUNDARY);
704     assert(gatewayData.sinkID!=0);
705     assert(gatewayData.sourceID!=0);
706     assert(gatewayData.controlDomainID!=0);
707     assert(gatewayData.domainSinkID!=0);
708     assert(gatewayData.domainSourceID!=0);
709     assert(!gatewayData.name.empty());
710     assert(!gatewayData.convertionMatrix.empty());
711     assert(!gatewayData.listSinkFormats.empty());
712     assert(!gatewayData.listSourceFormats.empty());
713
714     //might be that the sinks and sources are not there during registration time
715     //assert(existSink(gatewayData.sinkID));
716     //assert(existSource(gatewayData.sourceID));
717
718     am_gatewayID_t temp_GatewayID = 0;
719     am_gatewayID_t temp_GatewayIndex = 0;
720     //if gatewayData is zero and the first Static Sink was already entered, the ID is created
721     bool result;
722     if (gatewayData.gatewayID != 0 || mFirstStaticGateway)
723     {
724         //check if the ID already exists
725         if (existGateway(gatewayData.gatewayID))
726         {
727                 gatewayID = gatewayData.gatewayID;
728             return (E_ALREADY_EXISTS);
729         }
730     }
731     result = insertGatewayDB(gatewayData, temp_GatewayID);
732         if( false == result )
733                 return (E_UNKNOWN);
734
735         temp_GatewayIndex = temp_GatewayID;
736     //if the ID is not created, we add it to the query
737     if (gatewayData.gatewayID == 0 && mFirstStaticGateway)
738     {
739         mFirstStaticGateway = false;
740     }
741     mMappedData.mGatewayMap[temp_GatewayIndex].gatewayID = temp_GatewayID;
742     gatewayID = temp_GatewayID;
743
744     logInfo("DatabaseHandler::enterGatewayDB entered new gateway with name", gatewayData.name, "sourceID:", gatewayData.sourceID, "sinkID:", gatewayData.sinkID, "assigned ID:", gatewayID);
745     if (mpDatabaseObserver)
746         mpDatabaseObserver->newGateway(mMappedData.mGatewayMap[temp_GatewayIndex]);
747     return (E_OK);
748 }
749
750 void CAmDatabaseHandlerMap::dump( std::ostream & output ) const
751 {
752         output << std::endl << "****************** DUMP START ******************" << std::endl;
753         CAmMappedData::printMap(mMappedData.mDomainMap, output);
754         CAmMappedData::printMap(mMappedData.mSourceMap, output);
755         CAmMappedData::printMap(mMappedData.mSinkMap, output);
756         CAmMappedData::printMap(mMappedData.mSourceClassesMap, output);
757         CAmMappedData::printMap(mMappedData.mSinkClassesMap, output);
758         CAmMappedData::printMap(mMappedData.mConnectionMap, output);
759         CAmMappedData::printMap(mMappedData.mMainConnectionMap, output);
760         CAmMappedData::printMap(mMappedData.mCrossfaderMap, output);
761         CAmMappedData::printMap(mMappedData.mGatewayMap, output);
762         CAmVectorSystemProperties::const_iterator iter = mMappedData.mSystemProperties.begin();
763         output << "System properties" << "\n";
764         for(; iter!=mMappedData.mSystemProperties.end(); iter++)
765                 output << "[type:" << iter->type << " value:" << iter->value << "]";
766         output << std::endl << "****************** DUMP END ******************" << std::endl;
767 }
768
769 bool CAmDatabaseHandlerMap::insertSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID)
770 {
771     int16_t nextID = 0;
772         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentSourceID, sourceData.sourceID))
773         {
774                 sourceID = nextID;
775                 mMappedData.mSourceMap[nextID] = sourceData;
776                 mMappedData.mSourceMap[nextID].sourceID = nextID;
777                 return (true);
778         }
779         else
780         {
781                 sourceID = 0;
782                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
783                 return (false);
784         }
785 }
786
787 am_Error_e CAmDatabaseHandlerMap::enterSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID)
788 {
789     assert(sourceData.sourceID<DYNAMIC_ID_BOUNDARY);
790     assert(sourceData.domainID!=0);
791     assert(!sourceData.name.empty());
792     assert(sourceData.sourceClassID!=0);
793     // \todo: need to check if class exists?
794     assert(!sourceData.listConnectionFormats.empty());
795     assert(sourceData.sourceState>=SS_UNKNNOWN && sourceData.sourceState<=SS_MAX);
796
797     bool isFirstStatic = sourceData.sourceID == 0 && mFirstStaticSource;
798     am_sourceID_t temp_SourceID = 0;
799     am_sourceID_t temp_SourceIndex = 0;
800     bool checkForFlag = true;
801     CAmComparatorFlag comparator;
802     am_Source_Database_s const *reservedSource = findFirstObjectMatchingCriteria(mMappedData.mSourceMap, sourceData.name, comparator, &checkForFlag);
803         if( NULL != reservedSource )
804         {
805                 am_sourceID_t oldSourceID = reservedSource->sourceID;
806                 mMappedData.mSourceMap[oldSourceID] = sourceData;
807                 mMappedData.mSourceMap[oldSourceID].reserved = 0;
808                 temp_SourceID = oldSourceID;
809                 temp_SourceIndex = oldSourceID;
810         }
811         else
812         {
813             bool result;
814             if ( !isFirstStatic )
815             {
816                 //check if the ID already exists
817                  if (existSourceNameOrID(sourceData.sourceID, sourceData.name))
818                  {
819                         sourceID = sourceData.sourceID;
820                     return (E_ALREADY_EXISTS);
821                  }
822             }
823             result = insertSourceDB(sourceData, temp_SourceID);
824                 if( false == result )
825                         return (E_UNKNOWN);
826                 temp_SourceIndex = temp_SourceID;
827     }
828
829     if ( isFirstStatic )
830     {
831       //if the first static sink is entered, we need to set it onto the boundary if needed
832         mFirstStaticSource = false;
833     }
834     mMappedData.mSourceMap[temp_SourceIndex].sourceID = temp_SourceID;
835     sourceID = temp_SourceID;
836
837     logInfo("DatabaseHandler::enterSourceDB entered new source with name", sourceData.name, "domainID:", sourceData.domainID, "classID:", sourceData.sourceClassID, "visible:", sourceData.visible, "assigned ID:", sourceID);
838
839     if (mpDatabaseObserver)
840         mpDatabaseObserver->newSource(mMappedData.mSourceMap[temp_SourceIndex]);
841     return (E_OK);
842 }
843
844 am_Error_e CAmDatabaseHandlerMap::enterConnectionDB(const am_Connection_s& connection, am_connectionID_t& connectionID)
845 {
846     assert(connection.connectionID==0);
847     assert(connection.sinkID!=0);
848     assert(connection.sourceID!=0);
849     //connection format is not checked, because it's project specific
850     int16_t nextID = 0;
851     if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentConnectionID))
852         {
853                 connectionID = nextID;
854                 mMappedData.mConnectionMap[nextID] = connection;
855                 mMappedData.mConnectionMap[nextID].connectionID = nextID;
856         }
857         else
858         {
859                 connectionID = 0;
860                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
861                 return (E_UNKNOWN);
862         }
863
864     logInfo("DatabaseHandler::enterConnectionDB entered new connection sourceID=", connection.sourceID, "sinkID=", connection.sinkID, "sourceID=", connection.sourceID, "connectionFormat=", connection.connectionFormat, "assigned ID=", connectionID);
865     return (E_OK);
866 }
867
868 bool CAmDatabaseHandlerMap::insertSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID)
869 {
870     int16_t nextID = 0;
871         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentSinkClassesID, sinkClass.sinkClassID))
872         {
873                 sinkClassID = nextID;
874                 mMappedData.mSinkClassesMap[nextID] = sinkClass;
875                 mMappedData.mSinkClassesMap[nextID].sinkClassID = nextID;
876                 return (true);
877         }
878         else
879         {
880                 sinkClassID = 0;
881                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
882                 return (false);
883         }
884 }
885
886 am_Error_e CAmDatabaseHandlerMap::enterSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID)
887 {
888     assert(sinkClass.sinkClassID<DYNAMIC_ID_BOUNDARY);
889     assert(!sinkClass.name.empty());
890
891     am_sinkClass_t temp_SinkClassID = 0;
892     am_sinkClass_t temp_SinkClassIndex = 0;
893
894         bool result;
895         if (sinkClass.sinkClassID != 0 || mFirstStaticSinkClass)
896         {
897                 //check if the ID already exists
898                  if (existSinkClass(sinkClass.sinkClassID))
899                  {
900                          sinkClassID = sinkClass.sinkClassID;
901                         return (E_ALREADY_EXISTS);
902                  }
903         }
904         result = insertSinkClassDB(sinkClass, temp_SinkClassID);
905         if( false == result )
906                 return (E_UNKNOWN);
907
908         temp_SinkClassIndex = temp_SinkClassID;
909         //if the ID is not created, we add it to the query
910         if (sinkClass.sinkClassID == 0 && mFirstStaticSinkClass)
911         {
912                 mFirstStaticSinkClass = false;
913         }
914         mMappedData.mSinkClassesMap[temp_SinkClassIndex].sinkClassID = temp_SinkClassID;
915         sinkClassID = temp_SinkClassID;
916
917     //todo:change last_insert implementations for multithreaded usage...
918     logInfo("DatabaseHandler::enterSinkClassDB entered new sinkClass");
919     if (mpDatabaseObserver)
920         mpDatabaseObserver->numberOfSinkClassesChanged();
921     return (E_OK);
922 }
923
924 bool CAmDatabaseHandlerMap::insertSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass)
925 {
926     int16_t nextID = 0;
927         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentSourceClassesID, sourceClass.sourceClassID))
928         {
929                 sourceClassID = nextID;
930                 mMappedData.mSourceClassesMap[nextID] = sourceClass;
931                 mMappedData.mSourceClassesMap[nextID].sourceClassID = nextID;
932                 return (true);
933         }
934         else
935         {
936                 sourceClassID = 0;
937                 logInfo(__PRETTY_FUNCTION__,"Max limit reached.");
938                 return (false);
939         }
940 }
941
942 am_Error_e CAmDatabaseHandlerMap::enterSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass)
943 {
944     assert(sourceClass.sourceClassID<DYNAMIC_ID_BOUNDARY);
945     assert(!sourceClass.name.empty());
946
947     am_sourceClass_t temp_SourceClassID = 0;
948     am_sourceClass_t temp_SourceClassIndex = 0;
949
950         bool result;
951         if (sourceClass.sourceClassID != 0 || mFirstStaticSourceClass)
952         {
953                 //check if the ID already exists
954                 if (existSourceClass(sourceClass.sourceClassID))
955                 {
956                         sourceClassID = sourceClass.sourceClassID;
957                         return (E_ALREADY_EXISTS);
958                 }
959         }
960         result = insertSourceClassDB(temp_SourceClassID, sourceClass);
961         if( false == result )
962                 return (E_UNKNOWN);
963
964         temp_SourceClassIndex = temp_SourceClassID;
965         //if the ID is not created, we add it to the query
966         if (sourceClass.sourceClassID == 0 && mFirstStaticSourceClass)
967         {
968                 mFirstStaticSinkClass = false;
969         }
970         mMappedData.mSourceClassesMap[temp_SourceClassIndex].sourceClassID = temp_SourceClassID;
971         sourceClassID = temp_SourceClassID;
972
973         //todo:change last_insert implementations for multithread usage...
974
975     logInfo("DatabaseHandler::enterSourceClassDB entered new sourceClass");
976
977     if (mpDatabaseObserver)
978         mpDatabaseObserver->numberOfSourceClassesChanged();
979     return (E_OK);
980 }
981
982 am_Error_e CAmDatabaseHandlerMap::enterSystemProperties(const std::vector<am_SystemProperty_s> & listSystemProperties)
983 {
984     assert(!listSystemProperties.empty());
985
986     mMappedData.mSystemProperties = listSystemProperties;
987
988     logInfo("DatabaseHandler::enterSystemProperties entered system properties");
989     return (E_OK);
990 }
991
992 am_Error_e CAmDatabaseHandlerMap::changeMainConnectionRouteDB(const am_mainConnectionID_t mainconnectionID, const std::vector<am_connectionID_t>& listConnectionID)
993 {
994     assert(mainconnectionID!=0);
995     if (!existMainConnection(mainconnectionID))
996     {
997         return (E_NON_EXISTENT);
998     }
999
1000     int16_t delay = calculateDelayForRoute(listConnectionID);
1001
1002     //now we replace the data in the main connection object with the new one
1003     mMappedData.mMainConnectionMap[mainconnectionID].listConnectionID = listConnectionID;
1004
1005     if (changeDelayMainConnection(delay,mainconnectionID)!=E_OK)
1006         logError("DatabaseHandler::changeMainConnectionRouteDB error while changing mainConnectionDelay to ", delay);
1007
1008     logInfo("DatabaseHandler::changeMainConnectionRouteDB entered new route:", mainconnectionID);
1009     return (E_OK);
1010 }
1011
1012 am_Error_e CAmDatabaseHandlerMap::changeMainConnectionStateDB(const am_mainConnectionID_t mainconnectionID, const am_ConnectionState_e connectionState)
1013 {
1014     assert(mainconnectionID!=0);
1015     assert(connectionState>=CS_UNKNOWN && connectionState<=CS_MAX);
1016
1017     if (!existMainConnection(mainconnectionID))
1018     {
1019         return (E_NON_EXISTENT);
1020     }
1021     mMappedData.mMainConnectionMap[mainconnectionID].connectionState = connectionState;
1022
1023     logInfo("DatabaseHandler::changeMainConnectionStateDB changed mainConnectionState of MainConnection:", mainconnectionID, "to:", connectionState);
1024     if (mpDatabaseObserver)
1025         mpDatabaseObserver->mainConnectionStateChanged(mainconnectionID, connectionState);
1026     return (E_OK);
1027 }
1028
1029 am_Error_e CAmDatabaseHandlerMap::changeSinkMainVolumeDB(const am_mainVolume_t mainVolume, const am_sinkID_t sinkID)
1030 {
1031     assert(sinkID!=0);
1032
1033     if (!existSink(sinkID))
1034     {
1035         return (E_NON_EXISTENT);
1036     }
1037
1038     mMappedData.mSinkMap[sinkID].mainVolume = mainVolume;
1039
1040     logInfo("DatabaseHandler::changeSinkMainVolumeDB changed mainVolume of sink:", sinkID, "to:", mainVolume);
1041
1042     if (mpDatabaseObserver)
1043         mpDatabaseObserver->volumeChanged(sinkID, mainVolume);
1044
1045     return (E_OK);
1046 }
1047
1048 am_Error_e CAmDatabaseHandlerMap::changeSinkAvailabilityDB(const am_Availability_s & availability, const am_sinkID_t sinkID)
1049 {
1050     assert(sinkID!=0);
1051     assert(availability.availability>=A_UNKNOWN && availability.availability<=A_MAX);
1052     assert(availability.availabilityReason>=AR_UNKNOWN && availability.availabilityReason<=AR_MAX);
1053
1054     if (!existSink(sinkID))
1055     {
1056         return (E_NON_EXISTENT);
1057     }
1058
1059     mMappedData.mSinkMap[sinkID].available = availability;
1060
1061     logInfo("DatabaseHandler::changeSinkAvailabilityDB changed sinkAvailability of sink:", sinkID, "to:", availability.availability, "Reason:", availability.availabilityReason);
1062
1063     if (mpDatabaseObserver && sinkVisible(sinkID))
1064         mpDatabaseObserver->sinkAvailabilityChanged(sinkID, availability);
1065     return (E_OK);
1066 }
1067
1068 am_Error_e CAmDatabaseHandlerMap::changDomainStateDB(const am_DomainState_e domainState, const am_domainID_t domainID)
1069 {
1070     assert(domainID!=0);
1071     assert(domainState>=DS_UNKNOWN && domainState<=DS_MAX);
1072
1073     if (!existDomain(domainID))
1074     {
1075         return (E_NON_EXISTENT);
1076     }
1077
1078     mMappedData.mDomainMap[domainID].state = domainState;
1079
1080     logInfo("DatabaseHandler::changDomainStateDB changed domainState of domain:", domainID, "to:", domainState);
1081     return (E_OK);
1082 }
1083
1084 am_Error_e CAmDatabaseHandlerMap::changeSinkMuteStateDB(const am_MuteState_e muteState, const am_sinkID_t sinkID)
1085 {
1086     assert(sinkID!=0);
1087     assert(muteState>=MS_UNKNOWN && muteState<=MS_MAX);
1088
1089     if (!existSink(sinkID))
1090     {
1091         return (E_NON_EXISTENT);
1092     }
1093
1094     mMappedData.mSinkMap[sinkID].muteState = muteState;
1095
1096     logInfo("DatabaseHandler::changeSinkMuteStateDB changed sinkMuteState of sink:", sinkID, "to:", muteState);
1097
1098     if (mpDatabaseObserver)
1099         mpDatabaseObserver->sinkMuteStateChanged(sinkID, muteState);
1100
1101     return (E_OK);
1102 }
1103
1104 am_Error_e CAmDatabaseHandlerMap::changeMainSinkSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sinkID_t sinkID)
1105 {
1106     assert(soundProperty.type>=MSP_UNKNOWN && soundProperty.type<=MSP_MAX);
1107     assert(sinkID!=0);
1108
1109     if (!existSink(sinkID))
1110     {
1111         return (E_NON_EXISTENT);
1112     }
1113
1114     std::vector<am_MainSoundProperty_s>::iterator elementIterator = mMappedData.mSinkMap[sinkID].listMainSoundProperties.begin();
1115         for (;elementIterator != mMappedData.mSinkMap[sinkID].listMainSoundProperties.end(); ++elementIterator)
1116         {
1117                 if (elementIterator->type == soundProperty.type)
1118                         elementIterator->value = soundProperty.value;
1119         }
1120
1121     logInfo("DatabaseHandler::changeMainSinkSoundPropertyDB changed MainSinkSoundProperty of sink:", sinkID, "type:", soundProperty.type, "to:", soundProperty.value);
1122     if (mpDatabaseObserver)
1123         mpDatabaseObserver->mainSinkSoundPropertyChanged(sinkID, soundProperty);
1124     return (E_OK);
1125 }
1126
1127 am_Error_e CAmDatabaseHandlerMap::changeMainSourceSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sourceID_t sourceID)
1128 {
1129     assert(soundProperty.type>=MSP_UNKNOWN && soundProperty.type<=MSP_MAX);
1130     assert(sourceID!=0);
1131
1132     if (!existSource(sourceID))
1133     {
1134         return (E_NON_EXISTENT);
1135     }
1136
1137     std::vector<am_MainSoundProperty_s>::iterator elementIterator = mMappedData.mSourceMap[sourceID].listMainSoundProperties.begin();
1138         for (;elementIterator != mMappedData.mSourceMap[sourceID].listMainSoundProperties.end(); ++elementIterator)
1139         {
1140                 if (elementIterator->type == soundProperty.type)
1141                         elementIterator->value = soundProperty.value;
1142         }
1143
1144     logInfo("DatabaseHandler::changeMainSourceSoundPropertyDB changed MainSinkSoundProperty of source:", sourceID, "type:", soundProperty.type, "to:", soundProperty.value);
1145
1146     if (mpDatabaseObserver)
1147         mpDatabaseObserver->mainSourceSoundPropertyChanged(sourceID, soundProperty);
1148     return (E_OK);
1149 }
1150
1151 am_Error_e CAmDatabaseHandlerMap::changeSourceAvailabilityDB(const am_Availability_s & availability, const am_sourceID_t sourceID)
1152 {
1153     assert(sourceID!=0);
1154     assert(availability.availability>=A_UNKNOWN && availability.availability<=A_MAX);
1155     assert(availability.availabilityReason>=AR_UNKNOWN && availability.availabilityReason<=AR_MAX);
1156
1157     if (!existSource(sourceID))
1158     {
1159         return (E_NON_EXISTENT);
1160     }
1161
1162     mMappedData.mSourceMap[sourceID].available = availability;
1163
1164     logInfo("DatabaseHandler::changeSourceAvailabilityDB changed changeSourceAvailabilityDB of source:", sourceID, "to:", availability.availability, "Reason:", availability.availabilityReason);
1165
1166     if (mpDatabaseObserver && sourceVisible(sourceID))
1167         mpDatabaseObserver->sourceAvailabilityChanged(sourceID, availability);
1168     return (E_OK);
1169 }
1170
1171 am_Error_e CAmDatabaseHandlerMap::changeSystemPropertyDB(const am_SystemProperty_s & property)
1172 {
1173     assert(property.type>=SYP_UNKNOWN && property.type<=SYP_MAX);
1174
1175     std::vector<am_SystemProperty_s>::iterator elementIterator = mMappedData.mSystemProperties.begin();
1176         for (;elementIterator != mMappedData.mSystemProperties.end(); ++elementIterator)
1177         {
1178                 if (elementIterator->type == property.type)
1179                         elementIterator->value = property.value;
1180         }
1181
1182     logInfo("DatabaseHandler::changeSystemPropertyDB changed system property");
1183
1184     if (mpDatabaseObserver)
1185         mpDatabaseObserver->systemPropertyChanged(property);
1186
1187     return (E_OK);
1188 }
1189
1190 am_Error_e CAmDatabaseHandlerMap::removeMainConnectionDB(const am_mainConnectionID_t mainConnectionID)
1191 {
1192     assert(mainConnectionID!=0);
1193
1194     if (!existMainConnection(mainConnectionID))
1195     {
1196         return (E_NON_EXISTENT);
1197     }
1198
1199     mMappedData.mMainConnectionMap.erase(mainConnectionID);
1200
1201     logInfo("DatabaseHandler::removeMainConnectionDB removed:", mainConnectionID);
1202     if (mpDatabaseObserver)
1203     {
1204         mpDatabaseObserver->mainConnectionStateChanged(mainConnectionID, CS_DISCONNECTED);
1205         mpDatabaseObserver->removedMainConnection(mainConnectionID);
1206     }
1207     return (E_OK);
1208 }
1209
1210 am_Error_e CAmDatabaseHandlerMap::removeSinkDB(const am_sinkID_t sinkID)
1211 {
1212     assert(sinkID!=0);
1213
1214     if (!existSink(sinkID))
1215     {
1216         return (E_NON_EXISTENT);
1217     }
1218
1219     bool visible = sinkVisible(sinkID);
1220
1221     mMappedData.mSinkMap.erase(sinkID);
1222     // todo: Check the tables SinkMainSoundProperty and SinkMainNotificationConfiguration with 'visible' set to true
1223     //if visible is true then delete SinkMainSoundProperty and SinkMainNotificationConfiguration ????
1224     logInfo("DatabaseHandler::removeSinkDB removed:", sinkID);
1225
1226     if (mpDatabaseObserver != NULL)
1227         mpDatabaseObserver->removedSink(sinkID, visible);
1228
1229     return (E_OK);
1230 }
1231
1232 am_Error_e CAmDatabaseHandlerMap::removeSourceDB(const am_sourceID_t sourceID)
1233 {
1234     assert(sourceID!=0);
1235
1236     if (!existSource(sourceID))
1237     {
1238         return (E_NON_EXISTENT);
1239     }
1240
1241     bool visible = sourceVisible(sourceID);
1242
1243     mMappedData.mSourceMap.erase(sourceID);
1244
1245     // todo: Check the tables SourceMainSoundProperty and SourceMainNotificationConfiguration with 'visible' set to true
1246     //if visible is true then delete SourceMainSoundProperty and SourceMainNotificationConfiguration ????
1247
1248     logInfo("DatabaseHandler::removeSourceDB removed:", sourceID);
1249     if (mpDatabaseObserver)
1250         mpDatabaseObserver->removedSource(sourceID, visible);
1251     return (E_OK);
1252 }
1253
1254 am_Error_e CAmDatabaseHandlerMap::removeGatewayDB(const am_gatewayID_t gatewayID)
1255 {
1256     assert(gatewayID!=0);
1257
1258     if (!existGateway(gatewayID))
1259     {
1260         return (E_NON_EXISTENT);
1261     }
1262
1263     mMappedData.mGatewayMap.erase(gatewayID);
1264
1265     logInfo("DatabaseHandler::removeGatewayDB removed:", gatewayID);
1266     if (mpDatabaseObserver)
1267         mpDatabaseObserver->removeGateway(gatewayID);
1268     return (E_OK);
1269 }
1270
1271 am_Error_e CAmDatabaseHandlerMap::removeCrossfaderDB(const am_crossfaderID_t crossfaderID)
1272 {
1273     assert(crossfaderID!=0);
1274
1275     if (!existcrossFader(crossfaderID))
1276     {
1277         return (E_NON_EXISTENT);
1278     }
1279     mMappedData.mCrossfaderMap.erase(crossfaderID);
1280
1281     logInfo("DatabaseHandler::removeCrossfaderDB removed:", crossfaderID);
1282     if (mpDatabaseObserver)
1283         mpDatabaseObserver->removeCrossfader(crossfaderID);
1284     return (E_OK);
1285 }
1286
1287 am_Error_e CAmDatabaseHandlerMap::removeDomainDB(const am_domainID_t domainID)
1288 {
1289     assert(domainID!=0);
1290
1291     if (!existDomain(domainID))
1292     {
1293         return (E_NON_EXISTENT);
1294     }
1295     mMappedData.mDomainMap.erase(domainID);
1296
1297     logInfo("DatabaseHandler::removeDomainDB removed:", domainID);
1298     if (mpDatabaseObserver)
1299         mpDatabaseObserver->removeDomain(domainID);
1300     return (E_OK);
1301 }
1302
1303 am_Error_e CAmDatabaseHandlerMap::removeSinkClassDB(const am_sinkClass_t sinkClassID)
1304 {
1305     assert(sinkClassID!=0);
1306
1307     if (!existSinkClass(sinkClassID))
1308     {
1309         return (E_NON_EXISTENT);
1310     }
1311
1312     mMappedData.mSinkClassesMap.erase(sinkClassID);
1313
1314     logInfo("DatabaseHandler::removeSinkClassDB removed:", sinkClassID);
1315     if (mpDatabaseObserver)
1316         mpDatabaseObserver->numberOfSinkClassesChanged();
1317
1318     return (E_OK);
1319 }
1320
1321 am_Error_e CAmDatabaseHandlerMap::removeSourceClassDB(const am_sourceClass_t sourceClassID)
1322 {
1323     assert(sourceClassID!=0);
1324
1325     if (!existSourceClass(sourceClassID))
1326     {
1327         return (E_NON_EXISTENT);
1328     }
1329
1330     mMappedData.mSourceClassesMap.erase(sourceClassID);
1331     logInfo("DatabaseHandler::removeSourceClassDB removed:", sourceClassID);
1332     if (mpDatabaseObserver)
1333         mpDatabaseObserver->numberOfSourceClassesChanged();
1334     return (E_OK);
1335 }
1336
1337 am_Error_e CAmDatabaseHandlerMap::removeConnection(const am_connectionID_t connectionID)
1338 {
1339     assert(connectionID!=0);
1340
1341     mMappedData.mConnectionMap.erase(connectionID);
1342
1343     logInfo("DatabaseHandler::removeConnection removed:", connectionID);
1344     return (E_OK);
1345 }
1346
1347 am_Error_e CAmDatabaseHandlerMap::getSourceClassInfoDB(const am_sourceID_t sourceID, am_SourceClass_s & classInfo) const
1348 {
1349     assert(sourceID!=0);
1350
1351     if (!existSource(sourceID))
1352     {
1353         return (E_NON_EXISTENT);
1354     }
1355     am_Source_Database_s source = mMappedData.mSourceMap.at(sourceID);
1356     classInfo.sourceClassID  = source.sourceClassID;
1357
1358     if (!existSourceClass(classInfo.sourceClassID))
1359     {
1360         return (E_NON_EXISTENT);
1361     }
1362     am_SourceClass_s tmpClass = mMappedData.mSourceClassesMap.at(classInfo.sourceClassID);
1363     classInfo = tmpClass;
1364
1365     return (E_OK);
1366 }
1367
1368 am_Error_e CAmDatabaseHandlerMap::getSinkInfoDB(const am_sinkID_t sinkID, am_Sink_s & sinkData) const
1369 {
1370     assert(sinkID!=0);
1371
1372     if (!existSink(sinkID))
1373         return (E_NON_EXISTENT);
1374
1375     am_Sink_Database_s mappedSink = mMappedData.mSinkMap.at(sinkID);
1376     if( true == mappedSink.reserved )
1377         return (E_NON_EXISTENT);
1378         sinkData = mappedSink;
1379
1380     return (E_OK);
1381 }
1382
1383 am_Error_e CAmDatabaseHandlerMap::getSourceInfoDB(const am_sourceID_t sourceID, am_Source_s & sourceData) const
1384 {
1385     assert(sourceID!=0);
1386
1387     if (!existSource(sourceID))
1388         return (E_NON_EXISTENT);
1389
1390     am_Source_Database_s mappedSource = mMappedData.mSourceMap.at(sourceID);
1391     if( true == mappedSource.reserved )
1392         return (E_NON_EXISTENT);
1393
1394     sourceData = mappedSource;
1395
1396     return (E_OK);
1397 }
1398
1399 am_Error_e am::CAmDatabaseHandlerMap::getMainConnectionInfoDB(const am_mainConnectionID_t mainConnectionID, am_MainConnection_s & mainConnectionData) const
1400 {
1401     assert(mainConnectionID!=0);
1402     if (!existMainConnection(mainConnectionID))
1403     {
1404         return (E_NON_EXISTENT);
1405     }
1406     am_MainConnection_s temp = mMappedData.mMainConnectionMap.at(mainConnectionID);
1407     mainConnectionData = temp;
1408
1409     return (E_OK);
1410 }
1411
1412 am_Error_e CAmDatabaseHandlerMap::changeSinkClassInfoDB(const am_SinkClass_s& sinkClass)
1413 {
1414     assert(sinkClass.sinkClassID!=0);
1415     assert(!sinkClass.listClassProperties.empty());
1416
1417     //check if the ID already exists
1418     if (!existSinkClass(sinkClass.sinkClassID))
1419         return (E_NON_EXISTENT);
1420
1421     mMappedData.mSinkClassesMap[sinkClass.sinkClassID].listClassProperties = sinkClass.listClassProperties;
1422
1423     logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1424     return (E_OK);
1425 }
1426
1427 am_Error_e CAmDatabaseHandlerMap::changeSourceClassInfoDB(const am_SourceClass_s& sourceClass)
1428 {
1429     assert(sourceClass.sourceClassID!=0);
1430     assert(!sourceClass.listClassProperties.empty());
1431
1432     //check if the ID already exists
1433     if (!existSourceClass(sourceClass.sourceClassID))
1434         return (E_NON_EXISTENT);
1435
1436     mMappedData.mSourceClassesMap[sourceClass.sourceClassID].listClassProperties = sourceClass.listClassProperties;
1437
1438     logInfo("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo");
1439     return (E_OK);
1440 }
1441
1442 am_Error_e CAmDatabaseHandlerMap::getSinkClassInfoDB(const am_sinkID_t sinkID, am_SinkClass_s & sinkClass) const
1443 {
1444     assert(sinkID!=0);
1445
1446     if (!existSink(sinkID))
1447     {
1448         return (E_NON_EXISTENT);
1449     }
1450     am_Sink_Database_s sink = mMappedData.mSinkMap.at(sinkID);
1451     sinkClass.sinkClassID  = sink.sinkClassID;
1452
1453     if (!existSinkClass(sinkClass.sinkClassID))
1454     {
1455         return (E_NON_EXISTENT);
1456     }
1457     am_SinkClass_s tmpSinkClass = mMappedData.mSinkClassesMap.at(sinkClass.sinkClassID);
1458     sinkClass = tmpSinkClass;
1459
1460     return (E_OK);
1461 }
1462
1463 am_Error_e CAmDatabaseHandlerMap::getGatewayInfoDB(const am_gatewayID_t gatewayID, am_Gateway_s & gatewayData) const
1464 {
1465     assert(gatewayID!=0);
1466     if (!existGateway(gatewayID))
1467     {
1468         return (E_NON_EXISTENT);
1469     }
1470
1471     gatewayData = mMappedData.mGatewayMap.at(gatewayID);
1472
1473     return (E_OK);
1474
1475 }
1476
1477 am_Error_e CAmDatabaseHandlerMap::getCrossfaderInfoDB(const am_crossfaderID_t crossfaderID, am_Crossfader_s & crossfaderData) const
1478 {
1479     assert(crossfaderID!=0);
1480     if (!existcrossFader(crossfaderID))
1481     {
1482         return (E_NON_EXISTENT);
1483     }
1484
1485     crossfaderData = mMappedData.mCrossfaderMap.at(crossfaderID);
1486
1487     return (E_OK);
1488 }
1489
1490 am_Error_e CAmDatabaseHandlerMap::getListSinksOfDomain(const am_domainID_t domainID, std::vector<am_sinkID_t> & listSinkID) const
1491 {
1492     assert(domainID!=0);
1493     listSinkID.clear();
1494     if (!existDomain(domainID))
1495     {
1496         return (E_NON_EXISTENT);
1497     }
1498
1499     std::unordered_map<am_sinkID_t, am_Sink_Database_s>::const_iterator elementIterator = mMappedData.mSinkMap.begin();
1500         for (;elementIterator != mMappedData.mSinkMap.end(); ++elementIterator)
1501         {
1502                 if (0==elementIterator->second.reserved && domainID==elementIterator->second.domainID)
1503                         listSinkID.push_back(elementIterator->second.sinkID);
1504         }
1505     return (E_OK);
1506 }
1507
1508 am_Error_e CAmDatabaseHandlerMap::getListSourcesOfDomain(const am_domainID_t domainID, std::vector<am_sourceID_t> & listSourceID) const
1509 {
1510     assert(domainID!=0);
1511     listSourceID.clear();
1512     if (!existDomain(domainID))
1513     {
1514         return (E_NON_EXISTENT);
1515     }
1516      CAmMapSource::const_iterator elementIterator = mMappedData.mSourceMap.begin();
1517         for (;elementIterator != mMappedData.mSourceMap.end(); ++elementIterator)
1518         {
1519                 if (0==elementIterator->second.reserved && domainID==elementIterator->second.domainID)
1520                         listSourceID.push_back(elementIterator->second.sourceID);
1521         }
1522
1523     return (E_OK);
1524 }
1525
1526 am_Error_e CAmDatabaseHandlerMap::getListCrossfadersOfDomain(const am_domainID_t domainID, std::vector<am_crossfaderID_t> & listCrossfader) const
1527 {
1528     assert(domainID!=0);
1529     listCrossfader.clear();
1530     if (!existDomain(domainID))
1531     {
1532         return (E_NON_EXISTENT);
1533     }
1534
1535     CAmMapSource::const_iterator sourceIterator = mMappedData.mSourceMap.begin();
1536         for (;sourceIterator != mMappedData.mSourceMap.end(); ++sourceIterator)
1537         {
1538                 if (domainID==sourceIterator->second.domainID)
1539                 {
1540                         CAmMapCrossfader::const_iterator elementIterator = mMappedData.mCrossfaderMap.begin();
1541                         for (;elementIterator != mMappedData.mCrossfaderMap.end(); ++elementIterator)
1542                         {
1543                                 if ( sourceIterator->second.sourceID==elementIterator->second.sourceID )
1544                                         listCrossfader.push_back(elementIterator->second.crossfaderID);
1545                         }
1546                 }
1547         }
1548
1549     return (E_OK);
1550
1551 }
1552
1553 am_Error_e CAmDatabaseHandlerMap::getListGatewaysOfDomain(const am_domainID_t domainID, std::vector<am_gatewayID_t> & listGatewaysID) const
1554 {
1555     assert(domainID!=0);
1556     listGatewaysID.clear();
1557     if (!existDomain(domainID))
1558     {
1559         return (E_NON_EXISTENT);
1560     }
1561
1562     CAmMapGateway::const_iterator elementIterator = mMappedData.mGatewayMap.begin();
1563         for (;elementIterator != mMappedData.mGatewayMap.end(); ++elementIterator)
1564         {
1565                 if (domainID==elementIterator->second.controlDomainID)
1566                         listGatewaysID.push_back(elementIterator->second.gatewayID);
1567         }
1568     return (E_OK);
1569 }
1570
1571 am_Error_e CAmDatabaseHandlerMap::getListMainConnections(std::vector<am_MainConnection_s> & listMainConnections) const
1572 {
1573     listMainConnections.clear();
1574
1575     CAmMapMainConnection::const_iterator elementIterator = mMappedData.mMainConnectionMap.begin();
1576     for (;elementIterator != mMappedData.mMainConnectionMap.end(); ++elementIterator)
1577     {
1578         listMainConnections.push_back(elementIterator->second);
1579     }
1580
1581     return (E_OK);
1582 }
1583
1584 am_Error_e CAmDatabaseHandlerMap::getListDomains(std::vector<am_Domain_s> & listDomains) const
1585 {
1586     listDomains.clear();
1587
1588     CAmMapDomain::const_iterator elementIterator = mMappedData.mDomainMap.begin();
1589      for (;elementIterator != mMappedData.mDomainMap.end(); ++elementIterator)
1590      {
1591          if( 0==elementIterator->second.reserved )
1592                  listDomains.push_back(elementIterator->second);
1593      }
1594
1595     return (E_OK);
1596 }
1597
1598 am_Error_e CAmDatabaseHandlerMap::getListConnections(std::vector<am_Connection_s> & listConnections) const
1599 {
1600     listConnections.clear();
1601
1602     CAmMapConnection::const_iterator elementIterator = mMappedData.mConnectionMap.begin();
1603         for (;elementIterator != mMappedData.mConnectionMap.end(); ++elementIterator)
1604         {
1605                 if( 0==elementIterator->second.reserved )
1606                         listConnections.push_back(elementIterator->second);
1607         }
1608
1609       return (E_OK);
1610 }
1611
1612 am_Error_e CAmDatabaseHandlerMap::getListSinks(std::vector<am_Sink_s> & listSinks) const
1613 {
1614     listSinks.clear();
1615
1616         std::for_each(mMappedData.mSinkMap.begin(), mMappedData.mSinkMap.end(), [&](const std::pair<am_sinkID_t, am_Sink_Database_s>& ref) {
1617                 if( 0==ref.second.reserved )
1618                         listSinks.push_back(ref.second);
1619         });
1620
1621     return (E_OK);
1622 }
1623
1624 am_Error_e CAmDatabaseHandlerMap::getListSources(std::vector<am_Source_s> & listSources) const
1625 {
1626     listSources.clear();
1627
1628     std::for_each(mMappedData.mSourceMap.begin(), mMappedData.mSourceMap.end(), [&](const std::pair<am_sourceID_t, am_Source_Database_s>& ref) {
1629                 if( 0==ref.second.reserved )
1630                 {
1631                         listSources.push_back(ref.second);
1632                 }
1633         });
1634     return (E_OK);
1635 }
1636
1637 am_Error_e CAmDatabaseHandlerMap::getListSourceClasses(std::vector<am_SourceClass_s> & listSourceClasses) const
1638 {
1639     listSourceClasses.clear();
1640
1641     std::for_each(mMappedData.mSourceClassesMap.begin(), mMappedData.mSourceClassesMap.end(), [&](const std::pair<am_sourceClass_t, am_SourceClass_s>& ref) {
1642         listSourceClasses.push_back(ref.second);
1643      });
1644
1645     return (E_OK);
1646 }
1647
1648 am_Error_e CAmDatabaseHandlerMap::getListCrossfaders(std::vector<am_Crossfader_s> & listCrossfaders) const
1649 {
1650     listCrossfaders.clear();
1651
1652     std::for_each(mMappedData.mCrossfaderMap.begin(), mMappedData.mCrossfaderMap.end(), [&](const std::pair<am_crossfaderID_t, am_Crossfader_s>& ref) {
1653         listCrossfaders.push_back(ref.second);
1654        });
1655
1656     return (E_OK);
1657 }
1658
1659 am_Error_e CAmDatabaseHandlerMap::getListGateways(std::vector<am_Gateway_s> & listGateways) const
1660 {
1661     listGateways.clear();
1662
1663     std::for_each(mMappedData.mGatewayMap.begin(), mMappedData.mGatewayMap.end(), [&](const std::pair<am_gatewayID_t, am_Gateway_s>& ref) {
1664         listGateways.push_back(ref.second);
1665        });
1666
1667     return (E_OK);
1668 }
1669
1670 am_Error_e CAmDatabaseHandlerMap::getListSinkClasses(std::vector<am_SinkClass_s> & listSinkClasses) const
1671 {
1672     listSinkClasses.clear();
1673
1674     std::for_each(mMappedData.mSinkClassesMap.begin(), mMappedData.mSinkClassesMap.end(), [&](const std::pair<am_gatewayID_t, am_SinkClass_s>& ref) {
1675         listSinkClasses.push_back(ref.second);
1676        });
1677
1678     return (E_OK);
1679 }
1680
1681 am_Error_e CAmDatabaseHandlerMap::getListVisibleMainConnections(std::vector<am_MainConnectionType_s> & listConnections) const
1682 {
1683     listConnections.clear();
1684     am_MainConnectionType_s temp;
1685     std::for_each(mMappedData.mMainConnectionMap.begin(), mMappedData.mMainConnectionMap.end(), [&](const std::pair<am_mainConnectionID_t, am_MainConnection_Database_s>& ref) {
1686         ref.second.getMainConnectionType(temp);
1687         listConnections.push_back(temp);
1688       });
1689
1690     return (E_OK);
1691 }
1692
1693 am_Error_e CAmDatabaseHandlerMap::getListMainSinks(std::vector<am_SinkType_s> & listMainSinks) const
1694 {
1695     listMainSinks.clear();
1696     am_SinkType_s sinkType;
1697     std::for_each(mMappedData.mSinkMap.begin(), mMappedData.mSinkMap.end(), [&](const std::pair<am_sinkID_t, am_Sink_Database_s>& ref) {
1698         if( 0==ref.second.reserved && 1==ref.second.visible )
1699         {
1700                 ref.second.getSinkType(sinkType);
1701                 listMainSinks.push_back(sinkType);
1702         }
1703       });
1704
1705     return (E_OK);
1706 }
1707
1708 am_Error_e CAmDatabaseHandlerMap::getListMainSources(std::vector<am_SourceType_s> & listMainSources) const
1709 {
1710     listMainSources.clear();
1711     am_SourceType_s temp;
1712     std::for_each(mMappedData.mSourceMap.begin(), mMappedData.mSourceMap.end(), [&](const std::pair<am_sourceID_t, am_Source_Database_s>& ref) {
1713         if( 1==ref.second.visible )
1714         {
1715                 ref.second.getSourceType(temp);
1716                         listMainSources.push_back(temp);
1717         }
1718       });
1719
1720     return (E_OK);
1721 }
1722
1723 am_Error_e CAmDatabaseHandlerMap::getListMainSinkSoundProperties(const am_sinkID_t sinkID, std::vector<am_MainSoundProperty_s> & listSoundProperties) const
1724 {
1725     assert(sinkID!=0);
1726     if (!existSink(sinkID))
1727         return E_NON_EXISTENT;
1728
1729     am_Sink_s sink = mMappedData.mSinkMap.at(sinkID);
1730     listSoundProperties = sink.listMainSoundProperties;
1731
1732     return (E_OK);
1733 }
1734
1735 am_Error_e CAmDatabaseHandlerMap::getListMainSourceSoundProperties(const am_sourceID_t sourceID, std::vector<am_MainSoundProperty_s> & listSourceProperties) const
1736 {
1737     assert(sourceID!=0);
1738     if (!existSource(sourceID))
1739         return E_NON_EXISTENT;
1740
1741     am_Source_s source = mMappedData.mSourceMap.at(sourceID);
1742     listSourceProperties = source.listMainSoundProperties;
1743
1744     return (E_OK);
1745 }
1746
1747 am_Error_e CAmDatabaseHandlerMap::getListSystemProperties(std::vector<am_SystemProperty_s> & listSystemProperties) const
1748 {
1749      listSystemProperties = mMappedData.mSystemProperties;
1750     return (E_OK);
1751 }
1752
1753 am_Error_e am::CAmDatabaseHandlerMap::getListSinkConnectionFormats(const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
1754 {
1755    if (!existSink(sinkID))
1756            return E_NON_EXISTENT;
1757         am_Sink_s sink = mMappedData.mSinkMap.at(sinkID);
1758         listConnectionFormats = sink.listConnectionFormats;
1759
1760     return (E_OK);
1761 }
1762
1763 am_Error_e am::CAmDatabaseHandlerMap::getListSourceConnectionFormats(const am_sourceID_t sourceID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
1764 {
1765    if (!existSource(sourceID))
1766            return E_NON_EXISTENT;
1767     am_Source_s source = mMappedData.mSourceMap.at(sourceID);
1768     listConnectionFormats = source.listConnectionFormats;
1769
1770     return (E_OK);
1771 }
1772
1773 am_Error_e am::CAmDatabaseHandlerMap::getListGatewayConnectionFormats(const am_gatewayID_t gatewayID, std::vector<bool> & listConnectionFormat) const
1774 {
1775     ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
1776     iter = mListConnectionFormat.find(gatewayID);
1777     if (iter == mListConnectionFormat.end())
1778     {
1779         logError("DatabaseHandler::getListGatewayConnectionFormats database error with convertionFormat");
1780
1781         return E_NON_EXISTENT;
1782     }
1783     listConnectionFormat = iter->second;
1784
1785     return (E_OK);
1786 }
1787
1788 am_Error_e CAmDatabaseHandlerMap::getTimingInformation(const am_mainConnectionID_t mainConnectionID, am_timeSync_t & delay) const
1789 {
1790     assert(mainConnectionID!=0);
1791     if (!existMainConnection(mainConnectionID))
1792         return E_NON_EXISTENT;
1793     delay = -1;
1794
1795     am_MainConnection_s mainConnection = mMappedData.mMainConnectionMap.at(mainConnectionID);
1796     delay = mainConnection.delay;
1797
1798     if (delay == -1)
1799         return (E_NOT_POSSIBLE);
1800
1801     return (E_OK);
1802 }
1803
1804 am_Error_e CAmDatabaseHandlerMap::changeDelayMainConnection(const am_timeSync_t & delay, const am_mainConnectionID_t & connectionID)
1805 {
1806     assert(connectionID!=0);
1807     if (!existMainConnection(connectionID))
1808                 return (E_NON_EXISTENT);
1809     mMappedData.mMainConnectionMap[connectionID].delay = delay;
1810     if (mpDatabaseObserver)
1811         mpDatabaseObserver->timingInformationChanged(connectionID, delay);
1812     return (E_OK);
1813 }
1814
1815 /**
1816  * checks for a certain mainConnection
1817  * @param mainConnectionID to be checked for
1818  * @return true if it exists
1819  */
1820 bool CAmDatabaseHandlerMap::existMainConnection(const am_mainConnectionID_t mainConnectionID) const
1821 {
1822         return existsObjectWithKeyInMap(mainConnectionID, mMappedData.mMainConnectionMap);
1823 }
1824
1825 /**
1826  * checks for a certain Source
1827  * @param sourceID to be checked for
1828  * @return true if it exists
1829  */
1830 bool CAmDatabaseHandlerMap::existSource(const am_sourceID_t sourceID) const
1831 {
1832         am_Source_Database_s const * source = objectForKeyIfExistsInMap(sourceID, mMappedData.mSourceMap);
1833         if( NULL!=source )
1834                 return (0==source->reserved);
1835
1836         return false;
1837 }
1838
1839 /**
1840  * checks if a source name or ID exists
1841  * @param sourceID the sourceID
1842  * @param name the name
1843  * @return true if it exits
1844  */
1845 bool CAmDatabaseHandlerMap::existSourceNameOrID(const am_sourceID_t sourceID, const std::string & name) const
1846 {
1847     return sourceWithNameOrID(sourceID, name);
1848 }
1849
1850 /**
1851  * checks if a name exits
1852  * @param name the name
1853  * @return true if it exits
1854  */
1855 bool CAmDatabaseHandlerMap::existSourceName(const std::string & name) const
1856 {
1857     return existSourceNameOrID(SHRT_MAX, name);
1858 }
1859
1860 /**
1861  * checks for a certain Sink
1862  * @param sinkID to be checked for
1863  * @return true if it exists
1864  */
1865 bool CAmDatabaseHandlerMap::existSink(const am_sinkID_t sinkID) const
1866 {
1867         bool returnVal = false;
1868         CAmMapSink::const_iterator elementIterator = mMappedData.mSinkMap.begin();
1869         for (;elementIterator != mMappedData.mSinkMap.end(); ++elementIterator)
1870         {
1871                 if( 0==elementIterator->second.reserved &&
1872                         sinkID==elementIterator->second.sinkID)
1873                 {
1874                         returnVal = true;
1875                         break;
1876                 }
1877         }
1878     return (returnVal);
1879 }
1880
1881 /**
1882  * returns source with given ID or the name if exists
1883  * @param sourceID the ID
1884  * @param name the name
1885  * @return source structure if exists.
1886  */
1887 const CAmDatabaseHandlerMap::am_Source_Database_s *  CAmDatabaseHandlerMap::sourceWithNameOrID(const am_sourceID_t sourceID, const std::string & name) const
1888 {
1889         std::function<bool(const CAmDatabaseHandlerMap::am_Source_Database_s & refObject)> comparator = [&](const CAmDatabaseHandlerMap::am_Source_Database_s & source)->bool{
1890                         return ( 0==source.reserved &&
1891                                         (sourceID==source.sourceID || name.compare(source.name)==0));
1892         };
1893         return objectMatchingPredicate(mMappedData.mSourceMap, comparator);
1894 }
1895
1896 /**
1897  * returns sink with given ID or the name if exists
1898  * @param sinkID the ID
1899  * @param name the name
1900  * @return sink structure if exists.
1901  */
1902 const CAmDatabaseHandlerMap::am_Sink_Database_s * CAmDatabaseHandlerMap::sinkWithNameOrID(const am_sinkID_t sinkID, const std::string & name) const
1903 {
1904         std::function<bool(const CAmDatabaseHandlerMap::am_Sink_Database_s & refObject)> comparator = [&](const CAmDatabaseHandlerMap::am_Sink_Database_s & sink)->bool{
1905                         return ( 0==sink.reserved &&
1906                                         (sinkID==sink.sinkID || name.compare(sink.name)==0));
1907         };
1908         return objectMatchingPredicate(mMappedData.mSinkMap, comparator);
1909 }
1910
1911 /**
1912  * checks if a sink with the ID or the name exists
1913  * @param sinkID the ID
1914  * @param name the name
1915  * @return true if it exists.
1916  */
1917 bool CAmDatabaseHandlerMap::existSinkNameOrID(const am_sinkID_t sinkID, const std::string & name) const
1918 {
1919     return sinkWithNameOrID( sinkID,  name)!=NULL;
1920 }
1921
1922 /**
1923  * checks if a sink with the name exists
1924  * @param name the name
1925  * @return true if it exists
1926  */
1927 bool CAmDatabaseHandlerMap::existSinkName(const std::string & name) const
1928 {
1929     return existSinkNameOrID(SHRT_MAX, name);
1930 }
1931
1932 /**
1933  * checks for a certain domain
1934  * @param domainID to be checked for
1935  * @return true if it exists
1936  */
1937 bool CAmDatabaseHandlerMap::existDomain(const am_domainID_t domainID) const
1938 {
1939         am_Domain_Database_s const * source = objectForKeyIfExistsInMap(domainID, mMappedData.mDomainMap);
1940         if( NULL!=source )
1941                 return (0==source->reserved);
1942
1943         return false;
1944 }
1945
1946 /**
1947  * checks for certain gateway
1948  * @param gatewayID to be checked for
1949  * @return true if it exists
1950  */
1951 bool CAmDatabaseHandlerMap::existGateway(const am_gatewayID_t gatewayID) const
1952 {
1953         return existsObjectWithKeyInMap(gatewayID, mMappedData.mGatewayMap);
1954 }
1955
1956 am_Error_e CAmDatabaseHandlerMap::getDomainOfSource(const am_sourceID_t sourceID, am_domainID_t & domainID) const
1957 {
1958     assert(sourceID!=0);
1959
1960     am_Source_Database_s const * source = objectForKeyIfExistsInMap(sourceID, mMappedData.mSourceMap);
1961     if( NULL!=source )
1962     {
1963         domainID = source->domainID;
1964         return E_OK;
1965     }
1966     return E_NON_EXISTENT;
1967 }
1968
1969 am_Error_e am::CAmDatabaseHandlerMap::getDomainOfSink(const am_sinkID_t sinkID, am_domainID_t & domainID) const
1970 {
1971     assert(sinkID!=0);
1972
1973     am_Sink_Database_s const * source = objectForKeyIfExistsInMap(sinkID, mMappedData.mSinkMap);
1974         if( NULL!=source )
1975         {
1976                 domainID = source->domainID;
1977                 return E_OK;
1978         }
1979         return E_NON_EXISTENT;
1980 }
1981
1982 /**
1983  * checks for certain SinkClass
1984  * @param sinkClassID
1985  * @return true if it exists
1986  */
1987 bool CAmDatabaseHandlerMap::existSinkClass(const am_sinkClass_t sinkClassID) const
1988 {
1989         return existsObjectWithKeyInMap(sinkClassID, mMappedData.mSinkClassesMap);
1990 }
1991
1992 /**
1993  * checks for certain sourceClass
1994  * @param sourceClassID
1995  * @return true if it exists
1996  */
1997 bool CAmDatabaseHandlerMap::existSourceClass(const am_sourceClass_t sourceClassID) const
1998 {
1999         return existsObjectWithKeyInMap(sourceClassID, mMappedData.mSourceClassesMap);
2000 }
2001
2002 am_Error_e CAmDatabaseHandlerMap::changeConnectionTimingInformation(const am_connectionID_t connectionID, const am_timeSync_t delay)
2003 {
2004     assert(connectionID!=0);
2005     if(!existConnectionID(connectionID))
2006         return E_NON_EXISTENT;
2007
2008     mMappedData.mConnectionMap[connectionID].delay = delay;
2009
2010     //now we need to find all mainConnections that use the changed connection and update their timing
2011
2012     //first get all route tables for all mainconnections
2013
2014     CAmMapMainConnection::const_iterator iter = mMappedData.mMainConnectionMap.begin();
2015     for(; iter != mMappedData.mMainConnectionMap.end(); ++iter)
2016     {
2017         am_MainConnection_s mainConnection = iter->second;
2018         if (std::find(mainConnection.listConnectionID.begin(), mainConnection.listConnectionID.end(), connectionID) != mainConnection.listConnectionID.end())
2019         {
2020           // Got it.
2021                 changeDelayMainConnection(calculateMainConnectionDelay(mainConnection.mainConnectionID), mainConnection.mainConnectionID);
2022         }
2023
2024     }
2025
2026     return (E_OK);
2027 }
2028
2029 am_Error_e CAmDatabaseHandlerMap::changeConnectionFinal(const am_connectionID_t connectionID)
2030 {
2031     assert(connectionID!=0);
2032     am_Connection_Database_s const * connection = objectForKeyIfExistsInMap(connectionID, mMappedData.mConnectionMap);
2033     if( NULL!=connection )
2034     {
2035         mMappedData.mConnectionMap.at(connectionID).reserved = false;
2036         return E_OK;
2037     }
2038     return (E_NON_EXISTENT);
2039 }
2040
2041 am_timeSync_t CAmDatabaseHandlerMap::calculateMainConnectionDelay(const am_mainConnectionID_t mainConnectionID) const
2042 {
2043     assert(mainConnectionID!=0);
2044     if (!existMainConnection(mainConnectionID))
2045                 return -1;
2046     am_MainConnection_s mainConnection = mMappedData.mMainConnectionMap.at(mainConnectionID);
2047     am_timeSync_t delay = 0;
2048         am_timeSync_t min = SHRT_MAX;
2049     std::vector<am_connectionID_t>::const_iterator iter = mainConnection.listConnectionID.begin();
2050         for(;iter<mainConnection.listConnectionID.end(); ++iter)
2051         {
2052                 am_Connection_Database_s const * source = objectForKeyIfExistsInMap(*iter, mMappedData.mConnectionMap);
2053                 if( NULL!=source )
2054                 {
2055                         delay += source->delay;
2056                         min = std::min(min,source->delay);
2057                 }
2058         }
2059     if (min < 0)
2060         delay = -1;
2061     return (delay);
2062
2063 }
2064
2065 /**
2066  * registers the Observer at the Database
2067  * @param iObserver pointer to the observer
2068  */
2069 void CAmDatabaseHandlerMap::registerObserver(CAmDatabaseObserver *iObserver)
2070 {
2071     assert(iObserver!=NULL);
2072     mpDatabaseObserver = iObserver;
2073 }
2074
2075 /**
2076  * gives information about the visibility of a source
2077  * @param sourceID the sourceID
2078  * @return true if source is visible
2079  */
2080 bool CAmDatabaseHandlerMap::sourceVisible(const am_sourceID_t sourceID) const
2081 {
2082     assert(sourceID!=0);
2083     if (!existSource(sourceID))
2084         return false;
2085     am_Source_Database_s source = mMappedData.mSourceMap.at(sourceID);
2086     return source.visible;
2087 }
2088
2089 /**
2090  * gives information about the visibility of a sink
2091  * @param sinkID the sinkID
2092  * @return true if source is visible
2093  */
2094 bool CAmDatabaseHandlerMap::sinkVisible(const am_sinkID_t sinkID) const
2095 {
2096         am_Sink_Database_s const * source = objectForKeyIfExistsInMap(sinkID, mMappedData.mSinkMap);
2097         if( NULL!=source )
2098         {
2099                 if(0==source->reserved)
2100                         return source->visible;
2101         }
2102         return false;
2103 }
2104
2105 /**
2106  * checks if a connection already exists.
2107  * Only takes sink, source and format information for search!
2108  * @param connection the connection to be checked
2109  * @return true if connections exists
2110  */
2111 bool CAmDatabaseHandlerMap::existConnection(const am_Connection_s & connection) const
2112 {
2113         CAmComparatorFlag comparator;
2114         am_Connection_Database_s const * connectionObject = findFirstObjectMatchingCriteria(mMappedData.mConnectionMap, connection, comparator);
2115         return ( NULL!=connectionObject );
2116 }
2117
2118 /**
2119  * checks if a connection with the given ID exists
2120  * @param connectionID
2121  * @return true if connection exits
2122  */
2123 bool CAmDatabaseHandlerMap::existConnectionID(const am_connectionID_t connectionID) const
2124 {
2125         am_Connection_Database_s const * connection = objectForKeyIfExistsInMap(connectionID, mMappedData.mConnectionMap);
2126         if( NULL!=connection )
2127         {
2128                 return (0==connection->reserved);
2129         }
2130         return false;
2131 }
2132
2133 /**
2134  * checks if a CrossFader exists
2135  * @param crossfaderID the ID of the crossfader to be checked
2136  * @return true if exists
2137  */
2138 bool CAmDatabaseHandlerMap::existcrossFader(const am_crossfaderID_t crossfaderID) const
2139 {
2140      return existsObjectWithKeyInMap(crossfaderID, mMappedData.mCrossfaderMap);
2141 }
2142
2143 am_Error_e CAmDatabaseHandlerMap::getSoureState(const am_sourceID_t sourceID, am_SourceState_e & sourceState) const
2144 {
2145         am_Source_Database_s const * source = objectForKeyIfExistsInMap(sourceID, mMappedData.mSourceMap);
2146         if( NULL!=source )
2147         {
2148                 sourceState = source->sourceState;
2149                 return (E_OK);
2150         }
2151         else
2152         {
2153                 sourceState =  SS_UNKNNOWN;
2154                 return (E_NON_EXISTENT);
2155         }
2156 }
2157
2158 am_Error_e CAmDatabaseHandlerMap::changeSourceState(const am_sourceID_t sourceID, const am_SourceState_e sourceState)
2159 {
2160     assert(sourceID!=0);
2161     assert(sourceState>=SS_UNKNNOWN && sourceState<=SS_MAX);
2162     if(existSource(sourceID))
2163     {
2164         mMappedData.mSourceMap.at(sourceID).sourceState = sourceState;
2165                 return (E_OK);
2166         }
2167         return (E_NON_EXISTENT);
2168 }
2169
2170 am_Error_e CAmDatabaseHandlerMap::getSinkVolume(const am_sinkID_t sinkID, am_volume_t & volume) const
2171 {
2172     assert(sinkID!=0);
2173
2174         am_Sink_Database_s const * source = objectForKeyIfExistsInMap(sinkID, mMappedData.mSinkMap);
2175         if( NULL!=source )
2176         {
2177                 volume = source->volume;
2178                 return (E_OK);
2179         }
2180         volume = -1;
2181         return (E_NON_EXISTENT);
2182 }
2183
2184 am_Error_e CAmDatabaseHandlerMap::getSourceVolume(const am_sourceID_t sourceID, am_volume_t & volume) const
2185 {
2186     assert(sourceID!=0);
2187         am_Source_Database_s const * source = objectForKeyIfExistsInMap(sourceID, mMappedData.mSourceMap);
2188         if( NULL!=source )
2189         {
2190                 volume = source->volume;
2191                 return (E_OK);
2192         }
2193         volume = -1;
2194         return (E_NON_EXISTENT);
2195 }
2196
2197 am_Error_e CAmDatabaseHandlerMap::getSinkSoundPropertyValue(const am_sinkID_t sinkID, const am_SoundPropertyType_e propertyType, int16_t & value) const
2198 {
2199     assert(sinkID!=0);
2200
2201         am_Sink_Database_s const * source = objectForKeyIfExistsInMap(sinkID, mMappedData.mSinkMap);
2202         if( NULL!=source )
2203         {
2204                 std::vector<am_SoundProperty_s>::const_iterator iter = source->listSoundProperties.begin();
2205                 for(; iter<source->listSoundProperties.end(); ++iter)
2206                 {
2207                         if( propertyType == iter->type )
2208                         {
2209                                 value = iter->value;
2210                                 return (E_OK);
2211                         }
2212                 }
2213         }
2214         value = -1;
2215         return (E_NON_EXISTENT);
2216 }
2217
2218 am_Error_e CAmDatabaseHandlerMap::getSourceSoundPropertyValue(const am_sourceID_t sourceID, const am_SoundPropertyType_e propertyType, int16_t & value) const
2219 {
2220     assert(sourceID!=0);
2221
2222         am_Source_Database_s const * source = objectForKeyIfExistsInMap(sourceID, mMappedData.mSourceMap);
2223         if( NULL!=source )
2224         {
2225                 std::vector<am_SoundProperty_s>::const_iterator iter = source->listSoundProperties.begin();
2226                 for(; iter<source->listSoundProperties.end(); ++iter)
2227                 {
2228                         if( propertyType == iter->type )
2229                         {
2230                                 value = iter->value;
2231                                 return (E_OK);
2232                         }
2233                 }
2234         }
2235         value = -1;
2236         return (E_NON_EXISTENT);
2237 }
2238
2239 am_Error_e CAmDatabaseHandlerMap::getDomainState(const am_domainID_t domainID, am_DomainState_e& state) const
2240 {
2241     assert(domainID!=0);
2242
2243     am_Domain_Database_s const * source = objectForKeyIfExistsInMap(domainID, mMappedData.mDomainMap);
2244         if( NULL!=source )
2245         {
2246                 state = source->state;
2247                 return (E_OK);
2248         }
2249         state = DS_UNKNOWN;
2250         return (E_NON_EXISTENT);
2251 }
2252
2253 am_Error_e CAmDatabaseHandlerMap::peekDomain(const std::string & name, am_domainID_t & domainID)
2254 {
2255     domainID=0;
2256
2257     CAmComparator<am_Domain_Database_s> comparator;
2258     am_Domain_Database_s const *reservedDomain = findFirstObjectMatchingCriteria(mMappedData.mDomainMap, name, comparator);
2259
2260      if( NULL != reservedDomain )
2261     {
2262         domainID = reservedDomain->domainID;
2263         return E_OK;
2264     }
2265     else
2266     {
2267         int16_t nextID = 0;
2268         if( mMappedData.increaseID( &nextID, &mMappedData.mCurrentDomainID) )
2269         {
2270                 domainID = nextID;
2271                 am_Domain_Database_s domain;
2272                 domain.domainID = nextID;
2273                 domain.name = name;
2274                 domain.reserved = 1;
2275                 mMappedData.mDomainMap[nextID] = domain;
2276                 return E_OK;
2277         }
2278         return E_UNKNOWN;
2279     }
2280     return (E_OK);
2281 }
2282
2283 am_Error_e CAmDatabaseHandlerMap::peekSink(const std::string & name, am_sinkID_t & sinkID)
2284 {
2285         CAmComparator<am_Sink_Database_s> comparator;
2286         am_Sink_Database_s const *reservedSink = findFirstObjectMatchingCriteria(mMappedData.mSinkMap, name, comparator);
2287         if( NULL!=reservedSink )
2288     {
2289                 sinkID = reservedSink->sinkID;
2290         return E_OK;
2291     }
2292     else
2293     {
2294         int16_t nextID = 0;
2295         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentSinkID))
2296         {
2297                 if(mFirstStaticSink)
2298                 {
2299                         nextID = DYNAMIC_ID_BOUNDARY;
2300                         mFirstStaticSink = false;
2301                 }
2302                 sinkID = nextID;
2303                 am_Sink_Database_s object;
2304                 object.sinkID = nextID;
2305                 object.name = name;
2306                 object.reserved = 1;
2307                 mMappedData.mSinkMap[nextID] = object;
2308                 return E_OK;
2309         }
2310                 return E_UNKNOWN;
2311     }
2312 }
2313
2314 am_Error_e CAmDatabaseHandlerMap::peekSource(const std::string & name, am_sourceID_t & sourceID)
2315 {
2316         CAmComparator<am_Source_Database_s> comparator;
2317         am_Source_Database_s const *reservedDomain = findFirstObjectMatchingCriteria(mMappedData.mSourceMap, name, comparator);
2318         if( NULL!=reservedDomain )
2319     {
2320                 sourceID = reservedDomain->sourceID;
2321         return E_OK;
2322     }
2323     else
2324     {
2325         int16_t nextID = 0;
2326         if(mMappedData.increaseID(&nextID, &mMappedData.mCurrentSourceID))
2327         {
2328                 if(mFirstStaticSource)
2329                 {
2330 //                      nextID = DYNAMIC_ID_BOUNDARY;
2331                         mFirstStaticSource = false;
2332                 }
2333                 sourceID = nextID;
2334                 am_Source_Database_s object;
2335                 object.sourceID = nextID;
2336                 object.name = name;
2337                 object.reserved = 1;
2338                 mMappedData.mSourceMap[nextID] = object;
2339                 return E_OK;
2340         }
2341         else
2342                 return E_UNKNOWN;
2343     }
2344 }
2345
2346 am_Error_e CAmDatabaseHandlerMap::changeSinkVolume(const am_sinkID_t sinkID, const am_volume_t volume)
2347 {
2348     assert(sinkID!=0);
2349
2350     if (!existSink(sinkID))
2351     {
2352         return (E_NON_EXISTENT);
2353     }
2354
2355     mMappedData.mSinkMap[sinkID].volume = volume;
2356     return (E_OK);
2357 }
2358
2359 am_Error_e CAmDatabaseHandlerMap::changeSourceVolume(const am_sourceID_t sourceID, const am_volume_t volume)
2360 {
2361     assert(sourceID!=0);
2362     if (!existSource(sourceID))
2363     {
2364         return (E_NON_EXISTENT);
2365     }
2366     mMappedData.mSourceMap[sourceID].volume = volume;
2367
2368     return (E_OK);
2369 }
2370
2371 am_Error_e CAmDatabaseHandlerMap::changeSourceSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sourceID_t sourceID)
2372 {
2373     assert(soundProperty.type>=SP_UNKNOWN && soundProperty.type<=SP_MAX);
2374     assert(sourceID!=0);
2375
2376     if (!existSource(sourceID))
2377     {
2378         return (E_NON_EXISTENT);
2379     }
2380
2381     std::vector<am_SoundProperty_s>::iterator iter = mMappedData.mSourceMap[sourceID].listSoundProperties.begin();
2382         for(; iter<mMappedData.mSourceMap[sourceID].listSoundProperties.end(); ++iter)
2383         {
2384                 if( soundProperty.type == iter->type )
2385                 {
2386                         iter->value = soundProperty.value;
2387                         return (E_OK);
2388                 }
2389         }
2390         return (E_NON_EXISTENT);
2391 }
2392
2393 am_Error_e CAmDatabaseHandlerMap::changeSinkSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sinkID_t sinkID)
2394 {
2395     assert(soundProperty.type>=SP_UNKNOWN && soundProperty.type<=SP_MAX);
2396     assert(sinkID!=0);
2397
2398     if (!existSink(sinkID))
2399     {
2400         return (E_NON_EXISTENT);
2401     }
2402
2403     std::vector<am_SoundProperty_s>::iterator iter = mMappedData.mSinkMap[sinkID].listSoundProperties.begin();
2404         for(; iter<mMappedData.mSinkMap[sinkID].listSoundProperties.end(); ++iter)
2405         {
2406                 if( soundProperty.type == iter->type )
2407                 {
2408                         iter->value = soundProperty.value;
2409                         return (E_OK);
2410                 }
2411         }
2412         return (E_NON_EXISTENT);
2413 }
2414
2415 am_Error_e CAmDatabaseHandlerMap::changeCrossFaderHotSink(const am_crossfaderID_t crossfaderID, const am_HotSink_e hotsink)
2416 {
2417     assert(crossfaderID!=0);
2418     assert(hotsink>=HS_UNKNOWN && hotsink>=HS_MAX);
2419
2420     if (!existcrossFader(crossfaderID))
2421     {
2422         return (E_NON_EXISTENT);
2423     }
2424
2425     mMappedData.mCrossfaderMap[crossfaderID].hotSink = hotsink;
2426     return (E_OK);
2427 }
2428
2429 am_Error_e CAmDatabaseHandlerMap::getRoutingTree(bool onlyfree, CAmRoutingTree& tree, std::vector<CAmRoutingTreeItem*>& flatTree)
2430 {
2431     am_domainID_t rootID = tree.returnRootDomainID();
2432     CAmRoutingTreeItem *parent = tree.returnRootItem();
2433     size_t i = 0;
2434
2435     do
2436     {
2437         if (i != 0)
2438         {
2439             parent = flatTree.at(i - 1);
2440             rootID = parent->returnDomainID();
2441         }
2442                 std::for_each(mMappedData.mGatewayMap.begin(),mMappedData.mGatewayMap.end(), [&](const std::pair<am_gatewayID_t, am_Gateway_s>& refGateway) {
2443                         if( rootID==refGateway.second.domainSinkID )
2444                         {
2445                                 if(!onlyfree || std::find_if(mMappedData.mConnectionMap.begin(),
2446                                                                                          mMappedData.mConnectionMap.end(),
2447                                                                                          [&](const std::pair<am_connectionID_t, am_Connection_Database_s>& refConnection)
2448                                                                                          {
2449                                                                                                 return (refConnection.second.sinkID == refGateway.second.sinkID ||
2450                                                                                                                 refConnection.second.sourceID ==refGateway.second.sourceID);
2451                                                                                          })==mMappedData.mConnectionMap.end() )
2452                                 {
2453                                   // additional check to avoid cyclic routes
2454                                         const am_domainID_t domainSourceID = refGateway.second.domainSourceID;
2455                                         bool sourceDomainAlreadyHandledAsSink = false;
2456                                         for (std::vector<CAmRoutingTreeItem*>::const_iterator iFT = flatTree.begin(); iFT != flatTree.end(); ++iFT)
2457                                         {
2458                                                 if (domainSourceID == (*iFT)->returnParent()->returnDomainID())
2459                                                 {
2460                                                         sourceDomainAlreadyHandledAsSink = true;
2461                                                         break;
2462                                                 }
2463                                         }
2464
2465                                         if (!sourceDomainAlreadyHandledAsSink)
2466                                         {
2467                                                 // logInfo("DatabaseHandler::getRoutingTree ", rootID, ", ", domainSourceID, ", ", sqlite3_column_int(query, 1));
2468                                                 flatTree.push_back(tree.insertItem(domainSourceID, refGateway.second.gatewayID, parent));
2469                                         }
2470                                 }
2471                         }
2472                 });
2473         i++;
2474     } while (flatTree.size() > (i - 1));
2475
2476     return (E_OK);
2477 }
2478
2479 am_Error_e am::CAmDatabaseHandlerMap::peekSinkClassID(const std::string & name, am_sinkClass_t & sinkClassID)
2480 {
2481     if (name.empty())
2482         return (E_NON_EXISTENT);
2483     CAmComparator<am_SinkClass_Database_s> comparator;
2484     am_SinkClass_Database_s const *reservedDomain = findFirstObjectMatchingCriteria(mMappedData.mSinkClassesMap, name, comparator);
2485         if( NULL!=reservedDomain )
2486         {
2487                 sinkClassID = reservedDomain->sinkClassID;
2488                 return E_OK;
2489         }
2490         return (E_NON_EXISTENT);
2491 }
2492
2493 am_Error_e am::CAmDatabaseHandlerMap::peekSourceClassID(const std::string & name, am_sourceClass_t & sourceClassID)
2494 {
2495     if (name.empty())
2496         return (E_NON_EXISTENT);
2497     CAmComparator<am_SourceClass_Database_s> comparator;
2498     am_SourceClass_Database_s const *ptrSource = findFirstObjectMatchingCriteria(mMappedData.mSourceClassesMap, name, comparator);
2499         if( NULL!=ptrSource )
2500         {
2501                 sourceClassID = ptrSource->sourceClassID;
2502                 return E_OK;
2503         }
2504         return (E_NON_EXISTENT);
2505 }
2506
2507
2508 am_Error_e CAmDatabaseHandlerMap::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)
2509 {
2510     assert(sourceID!=0);
2511
2512     if (!existSource(sourceID))
2513     {
2514         return (E_NON_EXISTENT);
2515     }
2516     am_sourceClass_t sourceClassOut(sourceClassID);
2517     std::vector<am_MainSoundProperty_s> listMainSoundPropertiesOut(listMainSoundProperties);
2518     //check if sinkClass needs to be changed
2519
2520         std::unordered_map<am_sourceID_t, am_Source_Database_s>::iterator iter = mMappedData.mSourceMap.begin();
2521         for(; iter!=mMappedData.mSourceMap.end(); ++iter)
2522         {
2523                 if( iter->second.sourceID == sourceID )
2524                 {
2525                         if( sourceClassID!=0 )
2526                                 iter->second.sourceClassID = sourceClassID;
2527                         else if( 0 == iter->second.reserved )
2528                                 sourceClassOut = iter->second.sourceClassID;
2529                 }
2530         }
2531
2532     //check if soundProperties need to be updated
2533     if (!listSoundProperties.empty())
2534     {
2535         mMappedData.mSourceMap.at(sourceID).listSoundProperties = listSoundProperties;
2536     }
2537
2538     //check if we have to update the list of connectionformats
2539     if (!listConnectionFormats.empty())
2540     {
2541         mMappedData.mSourceMap.at(sourceID).listConnectionFormats = listConnectionFormats;
2542     }
2543
2544     //then we need to check if we need to update the listMainSoundProperties
2545     if (!listMainSoundProperties.empty() && sourceVisible(sourceID))
2546     {
2547
2548         mMappedData.mSourceMap.at(sourceID).listMainSoundProperties = listMainSoundProperties;
2549     }
2550     else //read out the properties
2551     {
2552         getListMainSourceSoundProperties(sourceID,listMainSoundPropertiesOut);
2553     }
2554
2555     logInfo("DatabaseHandler::changeSource changed changeSink of source:", sourceID);
2556
2557     if (mpDatabaseObserver != NULL)
2558     {
2559         mpDatabaseObserver->sourceUpdated(sourceID,sourceClassOut,listMainSoundPropertiesOut,sourceVisible(sourceID));
2560     }
2561
2562     return (E_OK);
2563
2564 }
2565
2566 am_Error_e CAmDatabaseHandlerMap::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)
2567 {
2568     assert(sinkID!=0);
2569
2570     am_sinkClass_t sinkClassOut(sinkClassID);
2571     std::vector<am_MainSoundProperty_s> listMainSoundPropertiesOut(listMainSoundProperties);
2572
2573     if (!existSink(sinkID))
2574     {
2575         return (E_NON_EXISTENT);
2576     }
2577
2578         std::unordered_map<am_sinkID_t, am_Sink_Database_s>::iterator iter = mMappedData.mSinkMap.begin();
2579         for(; iter!=mMappedData.mSinkMap.end(); ++iter)
2580         {
2581                 if( iter->second.sinkID == sinkID )
2582                 {
2583                         if( sinkClassID!=0 )
2584                                 iter->second.sinkClassID = sinkClassID;
2585                         else if( 0 == iter->second.reserved )
2586                                 sinkClassOut = iter->second.sinkClassID;
2587                 }
2588         }
2589
2590     //check if soundProperties need to be updated
2591     if (!listSoundProperties.empty())
2592     {
2593         mMappedData.mSinkMap.at(sinkID).listSoundProperties = listSoundProperties;
2594     }
2595
2596     //check if we have to update the list of connectionformats
2597     if (!listConnectionFormats.empty())
2598     {
2599         mMappedData.mSinkMap.at(sinkID).listConnectionFormats = listConnectionFormats;
2600     }
2601
2602     //then we need to check if we need to update the listMainSoundProperties
2603     if (!listMainSoundProperties.empty() && sinkVisible(sinkID))
2604     {
2605         mMappedData.mSinkMap.at(sinkID).listMainSoundProperties = listMainSoundProperties;
2606     }
2607     else //read out the properties
2608     {
2609         getListMainSinkSoundProperties(sinkID,listMainSoundPropertiesOut);
2610     }
2611
2612     logInfo("DatabaseHandler::changeSink changed changeSink of sink:", sinkID);
2613
2614     if (mpDatabaseObserver != NULL)
2615     {
2616         mpDatabaseObserver->sinkUpdated(sinkID,sinkClassOut,listMainSoundPropertiesOut,sinkVisible(sinkID));
2617     }
2618
2619     return (E_OK);
2620 }
2621
2622 am_Error_e CAmDatabaseHandlerMap::getListMainSinkNotificationConfigurations(const am_sinkID_t sinkID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations)
2623 {
2624     assert(sinkID!=0);
2625     if (!existSink(sinkID))
2626         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
2627     listMainNotificationConfigurations.clear();
2628
2629     listMainNotificationConfigurations = mMappedData.mSinkMap.at(sinkID).listMainNotificationConfigurations;
2630
2631      return (E_OK);
2632 }
2633
2634 am_Error_e CAmDatabaseHandlerMap::getListMainSourceNotificationConfigurations(const am_sourceID_t sourceID, std::vector<am_NotificationConfiguration_s>& listMainNotificationConfigurations)
2635 {
2636     assert(sourceID!=0);
2637     if (!existSource(sourceID))
2638         return (E_DATABASE_ERROR); // todo: here we could change to non existen, but not shown in sequences
2639
2640     listMainNotificationConfigurations = mMappedData.mSourceMap.at(sourceID).listMainNotificationConfigurations;
2641
2642     return (E_OK);
2643 }
2644
2645 bool changeMainNotificationConfiguration(std::vector<am_NotificationConfiguration_s> & listMainNotificationConfigurations,
2646                                                                                           const am_NotificationConfiguration_s & mainNotificationConfiguration)
2647 {
2648         bool changed = false;
2649     std::vector<am_NotificationConfiguration_s>::iterator iter = listMainNotificationConfigurations.begin();
2650         for(; iter<listMainNotificationConfigurations.end(); ++iter)
2651         {
2652                 if( mainNotificationConfiguration.type == iter->type )
2653                 {
2654                         iter->status = mainNotificationConfiguration.status;
2655                         iter->parameter = mainNotificationConfiguration.parameter;
2656                         changed |= true;
2657                 }
2658         }
2659         return changed;
2660 }
2661
2662 am_Error_e CAmDatabaseHandlerMap::changeMainSinkNotificationConfigurationDB(const am_sinkID_t sinkID, const am_NotificationConfiguration_s mainNotificationConfiguration)
2663 {
2664     assert(sinkID!=0);
2665
2666     if (!existSink(sinkID))
2667     {
2668         return (E_NON_EXISTENT);
2669     }
2670     if(!changeMainNotificationConfiguration(mMappedData.mSinkMap.at(sinkID).listMainNotificationConfigurations, mainNotificationConfiguration))
2671         return (E_NO_CHANGE);
2672
2673     logInfo("DatabaseHandler::changeMainSinkNotificationConfigurationDB changed MainNotificationConfiguration of source:", sinkID, "type:", mainNotificationConfiguration.type, "to status=", mainNotificationConfiguration.status, "and parameter=",mainNotificationConfiguration.parameter);
2674
2675     if (mpDatabaseObserver)
2676         mpDatabaseObserver->sinkMainNotificationConfigurationChanged(sinkID, mainNotificationConfiguration);
2677     return (E_OK);
2678 }
2679
2680 am_Error_e CAmDatabaseHandlerMap::changeMainSourceNotificationConfigurationDB(const am_sourceID_t sourceID, const am_NotificationConfiguration_s mainNotificationConfiguration)
2681 {
2682     assert(sourceID!=0);
2683
2684     if (!existSource(sourceID))
2685     {
2686         return (E_NON_EXISTENT);
2687     }
2688
2689     if(!changeMainNotificationConfiguration(mMappedData.mSourceMap.at(sourceID).listMainNotificationConfigurations, mainNotificationConfiguration))
2690         return (E_NO_CHANGE);
2691
2692     logInfo("DatabaseHandler::changeMainSourceNotificationConfigurationDB changed MainNotificationConfiguration of source:", sourceID, "type:", mainNotificationConfiguration.type, "to status=", mainNotificationConfiguration.status, "and parameter=",mainNotificationConfiguration.parameter);
2693
2694     if (mpDatabaseObserver)
2695         mpDatabaseObserver->sourceMainNotificationConfigurationChanged(sourceID, mainNotificationConfiguration);
2696     return (E_OK);
2697 }
2698
2699 am_Error_e CAmDatabaseHandlerMap::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)
2700 {
2701     assert(gatewayID!=0);
2702
2703    if (!existGateway(gatewayID))
2704    {
2705        return (E_NON_EXISTENT);
2706    }
2707
2708    if (!listSourceConnectionFormats.empty())
2709    {
2710            mMappedData.mGatewayMap.at(gatewayID).listSourceFormats = listSourceConnectionFormats;
2711    }
2712
2713    if (!listSinkConnectionFormats.empty())
2714    {
2715            mMappedData.mGatewayMap.at(gatewayID).listSinkFormats = listSinkConnectionFormats;
2716    }
2717
2718    if (!convertionMatrix.empty())
2719    {
2720        mListConnectionFormat.clear();
2721        mListConnectionFormat.insert(std::make_pair(gatewayID, convertionMatrix));
2722    }
2723
2724    logInfo("DatabaseHandler::changeGatewayDB changed Gateway with ID", gatewayID);
2725
2726    //todo: check if observer needs to be adopted.
2727    return (E_OK);
2728 }
2729
2730 bool changeNotificationConfiguration(std::vector<am_NotificationConfiguration_s> & listNotificationConfigurations, const am_NotificationConfiguration_s & notificationConfiguration)
2731 {
2732         bool changed = false;
2733     std::vector<am_NotificationConfiguration_s>::iterator iter = listNotificationConfigurations.begin();
2734         for(; iter<listNotificationConfigurations.end(); ++iter)
2735         {
2736                 if( notificationConfiguration.type == iter->type )
2737                 {
2738                         iter->status = notificationConfiguration.status;
2739                         iter->parameter = notificationConfiguration.parameter;
2740                         changed |= true;
2741                 }
2742         }
2743         return changed;
2744 }
2745
2746 am_Error_e CAmDatabaseHandlerMap::changeSinkNotificationConfigurationDB(const am_sinkID_t sinkID, const am_NotificationConfiguration_s notificationConfiguration)
2747 {
2748     assert(sinkID!=0);
2749
2750     if (!existSink(sinkID))
2751     {
2752         return (E_NON_EXISTENT);
2753     }
2754     if(!changeNotificationConfiguration(mMappedData.mSinkMap.at(sinkID).listNotificationConfigurations, notificationConfiguration))
2755         return (E_NO_CHANGE);
2756
2757     logInfo("DatabaseHandler::changeMainSinkNotificationConfigurationDB changed MainNotificationConfiguration of source:", sinkID, "type:", notificationConfiguration.type, "to status=", notificationConfiguration.status, "and parameter=",notificationConfiguration.parameter);
2758
2759     //todo:: inform obsever here...
2760     return (E_NON_EXISTENT);
2761 }
2762
2763 am_Error_e CAmDatabaseHandlerMap::changeSourceNotificationConfigurationDB(const am_sourceID_t sourceID, const am_NotificationConfiguration_s notificationConfiguration)
2764 {
2765     assert(sourceID!=0);
2766
2767     if (!existSource(sourceID))
2768     {
2769         return (E_NON_EXISTENT);
2770     }
2771
2772     if(!changeNotificationConfiguration(mMappedData.mSourceMap.at(sourceID).listNotificationConfigurations, notificationConfiguration))
2773         return (E_NO_CHANGE);
2774
2775     logInfo("DatabaseHandler::changeSourceNotificationConfigurationDB changed MainNotificationConfiguration of source:", sourceID, "type:", notificationConfiguration.type, "to status=", notificationConfiguration.status, "and parameter=",notificationConfiguration.parameter);
2776
2777     //todo:: implement observer function
2778     return (E_NON_EXISTENT);
2779 }
2780
2781 }