fb1e5c6d54ab861919ff1d169278c6ea450ec7da
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / DatabaseHandler.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file Databasehandler.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  */
24
25 #include "DatabaseHandler.h"
26 #include "DatabaseObserver.h"
27 #include <assert.h>
28 #include <vector>
29 #include <fstream>
30 #include <sstream>
31 #include <string>
32 #include <dlt/dlt.h>
33 #include "Router.h"
34
35 #define DOMAIN_TABLE "Domains"
36 #define SOURCE_CLASS_TABLE "SourceClasses"
37 #define SINK_CLASS_TABLE "SinkClasses"
38 #define SOURCE_TABLE "Sources"
39 #define SINK_TABLE "Sinks"
40 #define GATEWAY_TABLE "Gateways"
41 #define CROSSFADER_TABLE "Crossfaders"
42 #define CONNECTION_TABLE "Connections"
43 #define MAINCONNECTION_TABLE "MainConnections"
44 #define INTERRUPT_TABLE "Interrupts"
45 #define MAIN_TABLE "MainTable"
46 #define SYSTEM_TABLE "SystemProperties"
47
48 DLT_IMPORT_CONTEXT(AudioManager)
49
50 using namespace am;
51
52 const std::string databaseTables[] =
53 { //
54         " Domains (domainID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), busname VARCHAR(50), nodename VARCHAR(50), early BOOL, complete BOOL, state INTEGER, reserved BOOL);", " SourceClasses (sourceClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));", " SinkClasses (sinkClassID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50));",
55                 " Sources (sourceID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, domainID INTEGER, name VARCHAR(50), sourceClassID INTEGER, sourceState INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, interruptState INTEGER, reserved BOOL);", //
56                 " Sinks (sinkID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), domainID INTEGER, sinkClassID INTEGER, volume INTEGER, visible BOOL, availability INTEGER, availabilityReason INTEGER, muteState INTEGER, mainVolume INTEGER, reserved BOOL);", " Gateways (gatewayID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID INTEGER, sourceID INTEGER, domainSinkID INTEGER, domainSourceID INTEGER, controlDomainID INTEGER, inUse BOOL);", " Crossfaders (crossfaderID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), sinkID_A INTEGER, sinkID_B INTEGER, sourceID INTEGER, hotSink INTEGER);",
57                 " Connections (connectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, delay INTEGER, connectionFormat INTEGER, reserved BOOL);", //
58                 " MainConnections (mainConnectionID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sourceID INTEGER, sinkID INTEGER, connectionState INTEGER, delay INTEGER);", " SystemProperties (type INTEGER PRIMARY KEY, value INTEGER);" };
59
60 /**
61  * template to converts T to std::string
62  * @param i the value to be converted
63  * @return the string
64  */
65 template<typename T>
66 inline std::string i2s(T const& x)
67 {
68     std::ostringstream o;
69     o << x;
70     return o.str();
71 }
72
73 DatabaseHandler::DatabaseHandler(std::string databasePath) :
74         mDatabase(NULL), //
75         mPath(databasePath), //
76         mDatabaseObserver(NULL), //
77         mFirstStaticSink(true), //
78         mFirstStaticSource(true), //
79         mFirstStaticGateway(true), //
80         mFirstStaticSinkClass(true), //
81         mFirstStaticSourceClass(true), //
82         mListConnectionFormat()
83 {
84
85     /**
86      *\todo: this erases the database. just for testing!
87      */
88     std::ifstream infile(mPath.c_str());
89
90     if (infile)
91     {
92         remove(mPath.c_str());
93         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::DatabaseHandler Knocked down database"));
94     }
95
96     bool dbOpen = openDatabase();
97     if (!dbOpen)
98     {
99         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::DatabaseHandler problems opening the database!"));
100         assert(!dbOpen);
101     }
102
103     createTables();
104 }
105
106 DatabaseHandler::~DatabaseHandler()
107 {
108     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Closed Database"));
109     sqlite3_close(mDatabase);
110 }
111
112 am_Error_e DatabaseHandler::enterDomainDB(const am_Domain_s & domainData, am_domainID_t & domainID)
113 {
114     assert(domainData.domainID==0);
115     assert(!domainData.name.empty());
116     assert(!domainData.busname.empty());
117     assert(domainData.state>=DS_CONTROLLED && domainData.state<=DS_INDEPENDENT_RUNDOWN);
118
119     //first check for a reserved domain
120     sqlite3_stmt* query = NULL, *queryFinal;
121     int eCode = 0;
122     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
123     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
124     sqlite3_bind_text(query, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC);
125     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
126     {
127         command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET name=?, busname=?, nodename=?, early=?, complete=?, state=?, reserved=? WHERE domainID=" + i2s(sqlite3_column_int(query, 0));
128     }
129     else if (eCode == SQLITE_DONE)
130     {
131
132         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name, busname, nodename, early, complete, state, reserved) VALUES (?,?,?,?,?,?,?)";
133     }
134     else
135     {
136         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterDomainDB SQLITE Step error code:"), DLT_INT(eCode));
137         return E_DATABASE_ERROR;
138     }
139
140     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
141     {
142         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterDomainDB SQLITE Finalize error code:"), DLT_INT(eCode));
143         return E_DATABASE_ERROR;
144     }
145
146     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
147     sqlite3_bind_text(queryFinal, 1, domainData.name.c_str(), domainData.name.size(), SQLITE_STATIC);
148     sqlite3_bind_text(queryFinal, 2, domainData.busname.c_str(), domainData.busname.size(), SQLITE_STATIC);
149     sqlite3_bind_text(queryFinal, 3, domainData.nodename.c_str(), domainData.nodename.size(), SQLITE_STATIC);
150     sqlite3_bind_int(queryFinal, 4, domainData.early);
151     sqlite3_bind_int(queryFinal, 5, domainData.complete);
152     sqlite3_bind_int(queryFinal, 6, domainData.state);
153     sqlite3_bind_int(queryFinal, 7, 0);
154
155     if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
156     {
157         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterDomainDB SQLITE Step error code:"), DLT_INT(eCode));
158         return E_DATABASE_ERROR;
159     }
160
161     if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
162     {
163         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterDomainDB SQLITE Finalize error code:"), DLT_INT(eCode));
164         return E_DATABASE_ERROR;
165     }
166
167     domainID = sqlite3_last_insert_rowid(mDatabase);
168     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterDomainDB entered new domain with name"), DLT_STRING(domainData.name.c_str()), DLT_STRING("busname:"), DLT_STRING(domainData.busname.c_str()), DLT_STRING("nodename:"), DLT_STRING(domainData.nodename.c_str()), DLT_STRING("early:"), DLT_BOOL(domainData.early), DLT_STRING("complete:"), DLT_BOOL(domainData.complete), DLT_STRING("state:"), DLT_INT(domainData.state), DLT_STRING("assigned ID:"), DLT_INT16(domainID));
169
170     am_Domain_s domain = domainData;
171     domain.domainID = domainID;
172     if (mDatabaseObserver)
173         mDatabaseObserver->newDomain(domain);
174
175     return E_OK;
176 }
177
178 am_Error_e DatabaseHandler::enterMainConnectionDB(const am_MainConnection_s & mainConnectionData, am_mainConnectionID_t & connectionID)
179 {
180     assert(mainConnectionData.connectionID==0);
181     assert(mainConnectionData.connectionState>=CS_CONNECTING && mainConnectionData.connectionState<=CS_SUSPENDED);
182     assert(mainConnectionData.route.sinkID!=0);
183     assert(mainConnectionData.route.sourceID!=0);
184
185     sqlite3_stmt* query = NULL;
186     int eCode = 0;
187     std::string command = "INSERT INTO " + std::string(MAINCONNECTION_TABLE) + "(sourceID, sinkID, connectionState, delay) VALUES (?,?,?,-1)";
188     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
189     sqlite3_bind_int(query, 1, mainConnectionData.route.sourceID);
190     sqlite3_bind_int(query, 2, mainConnectionData.route.sinkID);
191     sqlite3_bind_int(query, 3, mainConnectionData.connectionState);
192
193     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
194     {
195         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:"), DLT_INT(eCode));
196         return E_DATABASE_ERROR;
197     }
198
199     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
200     {
201         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:"), DLT_INT(eCode));
202         return E_DATABASE_ERROR;
203     }
204
205     connectionID = sqlite3_last_insert_rowid(mDatabase);
206
207     //now check the connectionTable for all connections in the route. IF a particular route is not found, we return with error
208     std::vector<uint16_t> listOfConnections;
209     int16_t delay = 0;
210     command = "SELECT connectionID, delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE sourceID=? AND sinkID=? AND connectionFormat=?");
211     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
212     std::vector<am_RoutingElement_s>::const_iterator elementIterator = mainConnectionData.route.route.begin();
213     for (; elementIterator < mainConnectionData.route.route.end(); ++elementIterator)
214     {
215         sqlite3_bind_int(query, 1, elementIterator->sourceID);
216         sqlite3_bind_int(query, 2, elementIterator->sinkID);
217         sqlite3_bind_int(query, 3, elementIterator->connectionFormat);
218
219         if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
220         {
221             listOfConnections.push_back(sqlite3_column_int(query, 0));
222             int16_t temp_delay = sqlite3_column_int(query, 1);
223             if (temp_delay != -1 && delay != -1)
224                 delay += temp_delay;
225             else
226                 delay = -1;
227         }
228         else
229         {
230             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB did not find route for MainConnection:"), DLT_INT(eCode));
231             return E_DATABASE_ERROR;
232         }
233         sqlite3_reset(query);
234     }
235
236     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
237     {
238         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:"), DLT_INT(eCode));
239         return E_DATABASE_ERROR;
240     }
241
242     //now we create a table with references to the connections;
243     command = "CREATE TABLE MainConnectionRoute" + i2s(connectionID) + std::string("(connectionID INTEGER)");
244     assert(this->sqQuery(command));
245
246     command = "INSERT INTO MainConnectionRoute" + i2s(connectionID) + "(connectionID) VALUES (?)";
247     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
248     std::vector<uint16_t>::iterator listConnectionIterator = listOfConnections.begin();
249     for (; listConnectionIterator < listOfConnections.end(); ++listConnectionIterator)
250     {
251         sqlite3_bind_int(query, 1, *listConnectionIterator);
252         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
253         {
254             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB SQLITE Step error code:"), DLT_INT(eCode));
255             return E_DATABASE_ERROR;
256         }
257         sqlite3_reset(query);
258     }
259
260     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
261     {
262         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterMainConnectionDB SQLITE Finalize error code:"), DLT_INT(eCode));
263         return E_DATABASE_ERROR;
264     }
265
266     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterMainConnectionDB entered new mainConnection with sourceID"), DLT_INT(mainConnectionData.route.sourceID), DLT_STRING("sinkID:"), DLT_INT16(mainConnectionData.route.sinkID), DLT_STRING("delay:"), DLT_INT16(delay), DLT_STRING("assigned ID:"), DLT_INT16(connectionID));
267
268     if (mDatabaseObserver)
269     {
270         mDatabaseObserver->numberOfMainConnectionsChanged();
271         mDatabaseObserver->mainConnectionStateChanged(connectionID, mainConnectionData.connectionState);
272     }
273
274     //finally, we update the delay value for the maintable
275     if (delay == 0)
276         delay = -1;
277     return changeDelayMainConnection(delay, connectionID);
278 }
279
280 am_Error_e DatabaseHandler::enterSinkDB(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
281 {
282     assert(sinkData.sinkID<DYNAMIC_ID_BOUNDARY);
283     assert(sinkData.domainID!=0);
284     assert(!sinkData.name.empty());
285     assert(sinkData.sinkClassID!=0);
286     // \todo: need to check if class exists?
287     assert(!sinkData.listConnectionFormats.empty());
288     assert(sinkData.muteState>=MS_MUTED && sinkData.muteState<=MS_UNMUTED);
289
290     sqlite3_stmt *query = NULL, *queryFinal = NULL;
291     int eCode = 0;
292     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=? AND reserved=1";
293
294     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
295     sqlite3_bind_text(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
296
297     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
298     {
299         command = "UPDATE " + std::string(SINK_TABLE) + " SET name=?, domainID=?, sinkClassID=?, volume=?, visible=?, availability=?, availabilityReason=?, muteState=?, mainVolume=?, reserved=? WHERE sinkID=" + i2s(sqlite3_column_int(query, 0));
300     }
301     else if (eCode == SQLITE_DONE)
302     {
303         //if sinkID is zero and the first Static Sink was already entered, the ID is created
304         if (sinkData.sinkID == 0 && !mFirstStaticSink && !existSinkName(sinkData.name))
305         {
306             command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
307         }
308         else
309         {
310             //check if the ID already exists
311             if (existSinkNameOrID(sinkData.sinkID, sinkData.name))
312             {
313                 sqlite3_finalize(query);
314                 return E_ALREADY_EXISTS;
315             }
316             command = "INSERT INTO " + std::string(SINK_TABLE) + "(name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, reserved, sinkID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
317         }
318     }
319     else
320     {
321         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
322         sqlite3_finalize(query);
323         return E_DATABASE_ERROR;
324     }
325
326     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
327     {
328         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Finalize error code:"), DLT_INT(eCode));
329         return E_DATABASE_ERROR;
330     }
331
332     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
333     sqlite3_bind_text(queryFinal, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
334     sqlite3_bind_int(queryFinal, 2, sinkData.domainID);
335     sqlite3_bind_int(queryFinal, 3, sinkData.sinkClassID);
336     sqlite3_bind_int(queryFinal, 4, sinkData.volume);
337     sqlite3_bind_int(queryFinal, 5, sinkData.visible);
338     sqlite3_bind_int(queryFinal, 6, sinkData.available.availability);
339     sqlite3_bind_int(queryFinal, 7, sinkData.available.availabilityReason);
340     sqlite3_bind_int(queryFinal, 8, sinkData.muteState);
341     sqlite3_bind_int(queryFinal, 9, sinkData.mainVolume);
342     sqlite3_bind_int(queryFinal, 10, 0);
343
344     //if the ID is not created, we add it to the query
345     if (sinkData.sinkID != 0)
346     {
347         sqlite3_bind_int(queryFinal, 11, sinkData.sinkID);
348     }
349
350     //if the first static sink is entered, we need to set it onto the boundary
351     else if (mFirstStaticSink)
352     {
353         sqlite3_bind_int(queryFinal, 11, DYNAMIC_ID_BOUNDARY);
354         mFirstStaticSink = false;
355     }
356
357     if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
358     {
359         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
360         sqlite3_finalize(queryFinal);
361         return E_DATABASE_ERROR;
362     }
363
364     if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
365     {
366         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Finalize error code:"), DLT_INT(eCode));
367         return E_DATABASE_ERROR;
368     }
369
370     //now read back the sinkID
371     command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
372     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
373     sqlite3_bind_text(query, 1, sinkData.name.c_str(), sinkData.name.size(), SQLITE_STATIC);
374     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
375     {
376         sinkID = sqlite3_column_int(query, 0);
377     }
378     else
379     {
380         sinkID = 0;
381         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSink database error!:"), DLT_INT(eCode))
382         sqlite3_finalize(query);
383         return E_DATABASE_ERROR;
384     }
385     sqlite3_finalize(query);
386
387     //now we need to create the additional tables:
388     command = "CREATE TABLE SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat INTEGER)");
389     assert(this->sqQuery(command));
390     command = "CREATE TABLE SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
391     assert(this->sqQuery(command));
392     command = "CREATE TABLE SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
393     assert(this->sqQuery(command));
394
395     //fill ConnectionFormats
396     command = "INSERT INTO SinkConnectionFormat" + i2s(sinkID) + std::string("(soundFormat) VALUES (?)");
397     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
398     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = sinkData.listConnectionFormats.begin();
399     for (; connectionFormatIterator < sinkData.listConnectionFormats.end(); ++connectionFormatIterator)
400     {
401         sqlite3_bind_int(query, 1, *connectionFormatIterator);
402         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
403         {
404             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
405             sqlite3_finalize(query);
406             return E_DATABASE_ERROR;
407         }
408         sqlite3_reset(query);
409     }
410
411     //Fill MainSinkSoundProperties
412     command = "INSERT INTO SinkMainSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
413     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
414     std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sinkData.listMainSoundProperties.begin();
415     for (; mainSoundPropertyIterator < sinkData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
416     {
417         sqlite3_bind_int(query, 1, mainSoundPropertyIterator->type);
418         sqlite3_bind_int(query, 2, mainSoundPropertyIterator->value);
419         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
420         {
421             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
422             sqlite3_finalize(query);
423             return E_DATABASE_ERROR;
424         }
425         sqlite3_reset(query);
426     }
427
428     //Fill SinkSoundProperties
429     command = "INSERT INTO SinkSoundProperty" + i2s(sinkID) + std::string("(soundPropertyType,value) VALUES (?,?)");
430     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
431     std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sinkData.listSoundProperties.begin();
432     for (; SoundPropertyIterator < sinkData.listSoundProperties.end(); ++SoundPropertyIterator)
433     {
434         sqlite3_bind_int(query, 1, SoundPropertyIterator->type);
435         sqlite3_bind_int(query, 2, SoundPropertyIterator->value);
436         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
437         {
438             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
439             sqlite3_finalize(query);
440             return E_DATABASE_ERROR;
441         }
442         sqlite3_reset(query);
443     }
444
445     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterSinkDB entered new sink with name"), DLT_STRING(sinkData.name.c_str()), DLT_STRING("domainID:"), DLT_INT(sinkData.domainID), DLT_STRING("classID:"), DLT_INT(sinkData.sinkClassID), DLT_STRING("volume:"), DLT_INT(sinkData.volume), DLT_STRING("visible:"), DLT_BOOL(sinkData.visible), DLT_STRING("available.availability:"), DLT_INT(sinkData.available.availability), DLT_STRING("available.availabilityReason:"), DLT_INT(sinkData.available.availabilityReason), DLT_STRING("muteState:"), DLT_INT(sinkData.muteState), DLT_STRING("mainVolume:"), DLT_INT(sinkData.mainVolume), DLT_STRING("assigned ID:"), DLT_INT16(sinkID));
446
447     am_Sink_s sink = sinkData;
448     sink.sinkID = sinkID;
449     if (mDatabaseObserver != NULL)
450         mDatabaseObserver->newSink(sink);
451
452     return E_OK;
453 }
454
455 am_Error_e DatabaseHandler::enterCrossfaderDB(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
456 {
457     //todo: implement crossfader
458     (void) crossfaderData;
459     (void) crossfaderID;
460     return E_UNKNOWN;
461 }
462
463 am_Error_e DatabaseHandler::enterGatewayDB(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
464 {
465     assert(gatewayData.gatewayID<DYNAMIC_ID_BOUNDARY);
466     assert(gatewayData.sinkID!=0);
467     assert(gatewayData.sourceID!=0);
468     assert(gatewayData.controlDomainID!=0);
469     assert(gatewayData.domainSinkID!=0);
470     assert(gatewayData.domainSourceID!=0);
471     assert(!gatewayData.name.empty());
472     assert(!gatewayData.convertionMatrix.empty());
473     assert(!gatewayData.listSinkFormats.empty());
474     assert(!gatewayData.listSourceFormats.empty());
475
476     sqlite3_stmt* query = NULL;
477     int eCode = 0;
478     std::string command;
479
480     //if sinkID is zero and the first Static Sink was already entered, the ID is created
481     if (gatewayData.gatewayID == 0 && !mFirstStaticGateway)
482     {
483         command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, inUse) VALUES (?,?,?,?,?,?,0)";
484     }
485     else
486     {
487         //check if the ID already exists
488         if (existGateway(gatewayData.gatewayID))
489             return E_ALREADY_EXISTS;
490         command = "INSERT INTO " + std::string(GATEWAY_TABLE) + "(name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID, inUse) VALUES (?,?,?,?,?,?,?,0)";
491     }
492
493     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
494     sqlite3_bind_text(query, 1, gatewayData.name.c_str(), gatewayData.name.size(), SQLITE_STATIC);
495     sqlite3_bind_int(query, 2, gatewayData.sinkID);
496     sqlite3_bind_int(query, 3, gatewayData.sourceID);
497     sqlite3_bind_int(query, 4, gatewayData.domainSinkID);
498     sqlite3_bind_int(query, 5, gatewayData.domainSourceID);
499     sqlite3_bind_int(query, 6, gatewayData.controlDomainID);
500
501     //if the ID is not created, we add it to the query
502     if (gatewayData.gatewayID != 0)
503     {
504         sqlite3_bind_int(query, 7, gatewayData.gatewayID);
505     }
506
507     //if the first static sink is entered, we need to set it onto the boundary
508     else if (mFirstStaticGateway)
509     {
510         sqlite3_bind_int(query, 7, DYNAMIC_ID_BOUNDARY);
511         mFirstStaticGateway = false;
512     }
513
514     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
515     {
516         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterGatewayDB SQLITE Step error code:"), DLT_INT(eCode));
517         return E_DATABASE_ERROR;
518     }
519
520     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
521     {
522         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterGatewayDB SQLITE Finalize error code:"), DLT_INT(eCode));
523         return E_DATABASE_ERROR;
524     }
525
526     gatewayID = sqlite3_last_insert_rowid(mDatabase);
527
528     //now the convertion matrix todo: change the map implementation sometimes to blob in sqlite
529     mListConnectionFormat.insert(std::make_pair(gatewayID, gatewayData.convertionMatrix));
530
531     command = "CREATE TABLE GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
532     assert(this->sqQuery(command));
533     command = "CREATE TABLE GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat INTEGER)");
534     assert(this->sqQuery(command));
535
536     //fill ConnectionFormats
537     command = "INSERT INTO GatewaySourceFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
538     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
539     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = gatewayData.listSourceFormats.begin();
540     for (; connectionFormatIterator < gatewayData.listSourceFormats.end(); ++connectionFormatIterator)
541     {
542         sqlite3_bind_int(query, 1, *connectionFormatIterator);
543         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
544         {
545             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterGatewayDB SQLITE Step error code:"), DLT_INT(eCode));
546             return E_DATABASE_ERROR;
547         }
548         sqlite3_reset(query);
549     }
550
551     command = "INSERT INTO GatewaySinkFormat" + i2s(gatewayID) + std::string("(soundFormat) VALUES (?)");
552     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
553     connectionFormatIterator = gatewayData.listSinkFormats.begin();
554     for (; connectionFormatIterator < gatewayData.listSinkFormats.end(); ++connectionFormatIterator)
555     {
556         sqlite3_bind_int(query, 1, *connectionFormatIterator);
557         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
558         {
559             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterGatewayDB SQLITE Step error code:"), DLT_INT(eCode));
560             return E_DATABASE_ERROR;
561         }
562         sqlite3_reset(query);
563     }
564
565     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterGatewayDB entered new gateway with name"), DLT_STRING(gatewayData.name.c_str()), DLT_STRING("sourceID:"), DLT_INT(gatewayData.sourceID), DLT_STRING("sinkID:"), DLT_INT(gatewayData.sinkID), DLT_STRING("domainSinkID:"), DLT_INT(gatewayData.domainSinkID), DLT_STRING("domainSourceID:"), DLT_BOOL(gatewayData.domainSourceID), DLT_STRING("controlDomainID:"), DLT_INT(gatewayData.controlDomainID), DLT_STRING("assigned ID:"), DLT_INT16(gatewayID));
566
567     am_Gateway_s gateway = gatewayData;
568     gateway.gatewayID = gatewayID;
569     if (mDatabaseObserver)
570         mDatabaseObserver->newGateway(gateway);
571     return E_OK;
572 }
573
574 am_Error_e DatabaseHandler::enterSourceDB(const am_Source_s & sourceData, am_sourceID_t & sourceID)
575 {
576     assert(sourceData.sourceID<DYNAMIC_ID_BOUNDARY);
577     assert(sourceData.domainID!=0);
578     assert(!sourceData.name.empty());
579     assert(sourceData.sourceClassID!=0);
580     // \todo: need to check if class exists?
581     assert(!sourceData.listConnectionFormats.empty());
582     assert(sourceData.sourceState>=SS_ON && sourceData.sourceState<=SS_PAUSED);
583
584     sqlite3_stmt* query = NULL, *queryFinal = NULL;
585     ;
586     int eCode = 0;
587     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=? AND reserved=1";
588
589     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
590     sqlite3_bind_text(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
591
592     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
593     {
594         command = "UPDATE " + std::string(SOURCE_TABLE) + " SET name=?, domainID=?, sourceClassID=?, sourceState=?, volume=?, visible=?, availability=?, availabilityReason=?, interruptState=?, reserved=? WHERE sourceID=" + i2s(sqlite3_column_int(query, 0));
595     }
596     else if (eCode == SQLITE_DONE)
597     {
598         //if sinkID is zero and the first Static Sink was already entered, the ID is created
599         if (sourceData.sourceID == 0 && !mFirstStaticSource && !existSourceName(sourceData.name))
600         {
601             command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved) VALUES (?,?,?,?,?,?,?,?,?,?)";
602         }
603         else
604         {
605             //check if the ID already exists
606             if (existSourceNameOrID(sourceData.sourceID, sourceData.name))
607             {
608                 sqlite3_finalize(query);
609                 return E_ALREADY_EXISTS;
610             }
611             command = "INSERT INTO " + std::string(SOURCE_TABLE) + "(name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, reserved, sourceID) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
612         }
613     }
614     else
615     {
616         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Step error code:"), DLT_INT(eCode));
617         sqlite3_finalize(query);
618         return E_DATABASE_ERROR;
619     }
620
621     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
622     {
623         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Finalize error code:"), DLT_INT(eCode));
624         return E_DATABASE_ERROR;
625     }
626     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryFinal, NULL);
627     sqlite3_bind_text(queryFinal, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
628     sqlite3_bind_int(queryFinal, 2, sourceData.domainID);
629     sqlite3_bind_int(queryFinal, 3, sourceData.sourceClassID);
630     sqlite3_bind_int(queryFinal, 4, sourceData.sourceState);
631     sqlite3_bind_int(queryFinal, 5, sourceData.volume);
632     sqlite3_bind_int(queryFinal, 6, sourceData.visible);
633     sqlite3_bind_int(queryFinal, 7, sourceData.available.availability);
634     sqlite3_bind_int(queryFinal, 8, sourceData.available.availabilityReason);
635     sqlite3_bind_int(queryFinal, 9, sourceData.interruptState);
636     sqlite3_bind_int(queryFinal, 10, 0);
637
638     //if the ID is not created, we add it to the query
639     if (sourceData.sourceID != 0)
640     {
641         sqlite3_bind_int(queryFinal, 11, sourceData.sourceID);
642     }
643
644     //if the first static sink is entered, we need to set it onto the boundary
645     else if (mFirstStaticSource)
646     {
647         sqlite3_bind_int(queryFinal, 11, DYNAMIC_ID_BOUNDARY);
648         mFirstStaticSource = false;
649     }
650
651     if ((eCode = sqlite3_step(queryFinal)) != SQLITE_DONE)
652     {
653         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Step error code:"), DLT_INT(eCode));
654         sqlite3_finalize(queryFinal);
655         return E_DATABASE_ERROR;
656     }
657
658     if ((eCode = sqlite3_finalize(queryFinal)) != SQLITE_OK)
659     {
660         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Finalize error code:"), DLT_INT(eCode));
661         sqlite3_finalize(queryFinal);
662         return E_DATABASE_ERROR;
663     }
664
665     //now read back the sinkID
666     command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
667     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
668     sqlite3_bind_text(query, 1, sourceData.name.c_str(), sourceData.name.size(), SQLITE_STATIC);
669     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
670     {
671         sourceID = sqlite3_column_int(query, 0);
672     }
673     else
674     {
675         sourceID = 0;
676         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSink database error!:"), DLT_INT(eCode))
677         sqlite3_finalize(query);
678         return E_DATABASE_ERROR;
679     }
680     sqlite3_finalize(query);
681
682     //now we need to create the additional tables:
683     command = "CREATE TABLE SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat INTEGER)");
684     assert(this->sqQuery(command));
685     command = "CREATE TABLE SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
686     assert(this->sqQuery(command));
687     command = "CREATE TABLE SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType INTEGER, value INTEGER)");
688     assert(this->sqQuery(command));
689
690     //fill ConnectionFormats
691     command = "INSERT INTO SourceConnectionFormat" + i2s(sourceID) + std::string("(soundFormat) VALUES (?)");
692     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
693     std::vector<am_ConnectionFormat_e>::const_iterator connectionFormatIterator = sourceData.listConnectionFormats.begin();
694     for (; connectionFormatIterator < sourceData.listConnectionFormats.end(); ++connectionFormatIterator)
695     {
696         sqlite3_bind_int(query, 1, *connectionFormatIterator);
697         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
698         {
699             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Step error code:"), DLT_INT(eCode));
700             sqlite3_finalize(query);
701             return E_DATABASE_ERROR;
702         }
703         sqlite3_reset(query);
704     }
705
706     //Fill MainSinkSoundProperties
707     command = "INSERT INTO SourceMainSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
708     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
709     std::vector<am_MainSoundProperty_s>::const_iterator mainSoundPropertyIterator = sourceData.listMainSoundProperties.begin();
710     for (; mainSoundPropertyIterator < sourceData.listMainSoundProperties.end(); ++mainSoundPropertyIterator)
711     {
712         sqlite3_bind_int(query, 1, mainSoundPropertyIterator->type);
713         sqlite3_bind_int(query, 2, mainSoundPropertyIterator->value);
714         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
715         {
716             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceDB SQLITE Step error code:"), DLT_INT(eCode));
717             sqlite3_finalize(query);
718             return E_DATABASE_ERROR;
719         }
720         sqlite3_reset(query);
721     }
722
723     //Fill SinkSoundProperties
724     command = "INSERT INTO SourceSoundProperty" + i2s(sourceID) + std::string("(soundPropertyType,value) VALUES (?,?)");
725     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
726     std::vector<am_SoundProperty_s>::const_iterator SoundPropertyIterator = sourceData.listSoundProperties.begin();
727     for (; SoundPropertyIterator < sourceData.listSoundProperties.end(); ++SoundPropertyIterator)
728     {
729         sqlite3_bind_int(query, 1, SoundPropertyIterator->type);
730         sqlite3_bind_int(query, 2, SoundPropertyIterator->value);
731         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
732         {
733             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkDB SQLITE Step error code:"), DLT_INT(eCode));
734             sqlite3_finalize(query);
735             return E_DATABASE_ERROR;
736         }
737         sqlite3_reset(query);
738     }
739
740     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterSinkDB entered new source with name"), DLT_STRING(sourceData.name.c_str()), DLT_STRING("domainID:"), DLT_INT(sourceData.domainID), DLT_STRING("classID:"), DLT_INT(sourceData.sourceClassID), DLT_STRING("volume:"), DLT_INT(sourceData.volume), DLT_STRING("visible:"), DLT_BOOL(sourceData.visible), DLT_STRING("available.availability:"), DLT_INT(sourceData.available.availability), DLT_STRING("available.availabilityReason:"), DLT_INT(sourceData.available.availabilityReason), DLT_STRING("interruptState:"), DLT_INT(sourceData.interruptState), DLT_STRING("assigned ID:"), DLT_INT16(sourceID));
741
742     am_Source_s source = sourceData;
743     source.sourceID = sourceID;
744     if (mDatabaseObserver)
745         mDatabaseObserver->newSource(source);
746     return E_OK;
747 }
748
749 am_Error_e DatabaseHandler::changeMainConnectionRouteDB(const am_mainConnectionID_t mainconnectionID, const am_Route_s & route)
750 {
751     assert(mainconnectionID!=0);
752     if (!existMainConnection(mainconnectionID))
753     {
754         return E_NON_EXISTENT;
755     }
756     sqlite3_stmt* query = NULL;
757     int eCode = 0;
758     std::string command;
759
760     std::vector<uint16_t> listOfConnections;
761     int16_t delay = 0;
762     command = "SELECT connectionID, delay FROM " + std::string(CONNECTION_TABLE) + (" WHERE sourceID=? AND sinkID=? AND connectionFormat=?");
763     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
764     std::vector<am_RoutingElement_s>::const_iterator elementIterator = route.route.begin();
765     for (; elementIterator < route.route.end(); ++elementIterator)
766     {
767         sqlite3_bind_int(query, 1, elementIterator->sourceID);
768         sqlite3_bind_int(query, 2, elementIterator->sinkID);
769         sqlite3_bind_int(query, 3, elementIterator->connectionFormat);
770
771         if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
772         {
773             listOfConnections.push_back(sqlite3_column_int(query, 0));
774             int16_t temp_delay = sqlite3_column_int(query, 1);
775             if (temp_delay != -1 && delay != -1)
776                 delay += temp_delay;
777             else
778                 delay = -1;
779         }
780         else
781         {
782             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionRouteDB did not find route for MainConnection:"), DLT_INT(eCode));
783             return E_DATABASE_ERROR;
784         }
785         sqlite3_reset(query);
786     }
787
788     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
789     {
790         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionRouteDB SQLITE Finalize error code:"), DLT_INT(eCode));
791         return E_DATABASE_ERROR;
792     }
793
794     //now we delete the data in the table
795     command = "DELETE from MainConnectionRoute" + i2s(mainconnectionID);
796     assert(this->sqQuery(command));
797
798     command = "INSERT INTO MainConnectionRoute" + i2s(mainconnectionID) + "(connectionID) VALUES (?)";
799     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
800     std::vector<uint16_t>::iterator listConnectionIterator = listOfConnections.begin();
801     for (; listConnectionIterator < listOfConnections.end(); ++listConnectionIterator)
802     {
803         sqlite3_bind_int(query, 1, *listConnectionIterator);
804         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
805         {
806             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionRouteDB SQLITE Step error code:"), DLT_INT(eCode));
807             return E_DATABASE_ERROR;
808         }
809         sqlite3_reset(query);
810     }
811
812     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
813     {
814         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionRouteDB SQLITE Finalize error code:"), DLT_INT(eCode));
815         return E_DATABASE_ERROR;
816     }DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeMainConnectionRouteDB entered new route:"), DLT_INT(mainconnectionID));
817     return E_OK;
818 }
819
820 am_Error_e DatabaseHandler::changeMainConnectionStateDB(const am_mainConnectionID_t mainconnectionID, const am_ConnectionState_e connectionState)
821 {
822     assert(mainconnectionID!=0);
823
824     sqlite3_stmt* query = NULL;
825     int eCode = 0;
826     std::string command;
827
828     if (!existMainConnection(mainconnectionID))
829     {
830         return E_NON_EXISTENT;
831     }
832     command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET connectionState=? WHERE mainConnectionID=" + i2s(mainconnectionID);
833     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
834     sqlite3_bind_int(query, 1, connectionState);
835     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
836     {
837         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionStateDB SQLITE Step error code:"), DLT_INT(eCode));
838         return E_DATABASE_ERROR;
839     }
840     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
841     {
842         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainConnectionStateDB SQLITE Finalize error code:"), DLT_INT(eCode));
843         return E_DATABASE_ERROR;
844     }DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeMainConnectionStateDB changed mainConnectionState of MainConnection:"), DLT_INT(mainconnectionID), DLT_STRING("to:"), DLT_INT(connectionState));
845
846     if (mDatabaseObserver)
847         mDatabaseObserver->mainConnectionStateChanged(mainconnectionID, connectionState);
848     return E_OK;
849 }
850
851 am_Error_e DatabaseHandler::changeSinkMainVolumeDB(const am_mainVolume_t mainVolume, const am_sinkID_t sinkID)
852 {
853     assert(sinkID!=0);
854
855     sqlite3_stmt* query = NULL;
856     int eCode = 0;
857     std::string command;
858
859     if (!existSink(sinkID))
860     {
861         return E_NON_EXISTENT;
862     }
863     command = "UPDATE " + std::string(SINK_TABLE) + " SET mainVolume=? WHERE sinkID=" + i2s(sinkID);
864     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
865     sqlite3_bind_int(query, 1, mainVolume);
866     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
867     {
868         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkMainVolumeDB SQLITE Step error code:"), DLT_INT(eCode));
869         return E_DATABASE_ERROR;
870     }
871     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
872     {
873         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkMainVolumeDB SQLITE Finalize error code:"), DLT_INT(eCode));
874         return E_DATABASE_ERROR;
875     }
876
877     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSinkMainVolumeDB changed mainVolume of sink:"), DLT_INT(sinkID), DLT_STRING("to:"), DLT_INT(mainVolume));
878
879     if (mDatabaseObserver)
880         mDatabaseObserver->volumeChanged(sinkID, mainVolume);
881
882     return E_OK;
883 }
884
885 am_Error_e DatabaseHandler::changeSinkAvailabilityDB(const am_Availability_s & availability, const am_sinkID_t sinkID)
886 {
887     assert(sinkID!=0);
888
889     sqlite3_stmt* query = NULL;
890     int eCode = 0;
891     std::string command;
892
893     if (!existSink(sinkID))
894     {
895         return E_NON_EXISTENT;
896     }
897     command = "UPDATE " + std::string(SINK_TABLE) + " SET availability=?, availabilityReason=? WHERE sinkID=" + i2s(sinkID);
898     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
899     sqlite3_bind_int(query, 1, availability.availability);
900     sqlite3_bind_int(query, 2, availability.availabilityReason);
901     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
902     {
903         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkAvailabilityDB SQLITE Step error code:"), DLT_INT(eCode));
904         return E_DATABASE_ERROR;
905     }assert(sinkID!=0);
906
907     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
908     {
909         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkAvailabilityDB SQLITE Finalize error code:"), DLT_INT(eCode));
910         return E_DATABASE_ERROR;
911     }
912
913     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSinkAvailabilityDB changed sinkAvailability of sink:"), DLT_INT(sinkID), DLT_STRING("to:"), DLT_INT(availability.availability), DLT_STRING("Reason:"), DLT_INT(availability.availabilityReason));
914
915     if (mDatabaseObserver && sourceVisible(sinkID))
916         mDatabaseObserver->sinkAvailabilityChanged(sinkID, availability);
917     return E_OK;
918 }
919
920 am_Error_e DatabaseHandler::changDomainStateDB(const am_DomainState_e domainState, const am_domainID_t domainID)
921 {
922     assert(domainID!=0);
923
924     sqlite3_stmt* query = NULL;
925     int eCode = 0;
926     std::string command;
927
928     if (!existDomain(domainID))
929     {
930         return E_NON_EXISTENT;
931     }
932     command = "UPDATE " + std::string(DOMAIN_TABLE) + " SET state=? WHERE domainID=" + i2s(domainID);
933     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
934     sqlite3_bind_int(query, 1, domainState);
935     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
936     {
937         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changDomainStateDB SQLITE Step error code:"), DLT_INT(eCode));
938         return E_DATABASE_ERROR;
939     }
940
941     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
942     {
943         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changDomainStateDB SQLITE Finalize error code:"), DLT_INT(eCode));
944         return E_DATABASE_ERROR;
945     }
946
947     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changDomainStateDB changed domainState of domain:"), DLT_INT(domainID), DLT_STRING("to:"), DLT_INT(domainState));
948     return E_OK;
949 }
950
951 am_Error_e DatabaseHandler::changeSinkMuteStateDB(const am_MuteState_e muteState, const am_sinkID_t sinkID)
952 {
953     assert(sinkID!=0);
954
955     sqlite3_stmt* query = NULL;
956     int eCode = 0;
957     std::string command;
958
959     if (!existSink(sinkID))
960     {
961         return E_NON_EXISTENT;
962     }
963     command = "UPDATE " + std::string(SINK_TABLE) + " SET muteState=? WHERE sinkID=" + i2s(sinkID);
964     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
965     sqlite3_bind_int(query, 1, muteState);
966     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
967     {
968         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkMuteStateDB SQLITE Step error code:"), DLT_INT(eCode));
969         return E_DATABASE_ERROR;
970     }assert(sinkID!=0);
971
972     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
973     {
974         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkMuteStateDB SQLITE Finalize error code:"), DLT_INT(eCode));
975         return E_DATABASE_ERROR;
976     }
977
978     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSinkMuteStateDB changed sinkMuteState of sink:"), DLT_INT(sinkID), DLT_STRING("to:"), DLT_INT(muteState));
979
980     if (mDatabaseObserver)
981         mDatabaseObserver->sinkMuteStateChanged(sinkID, muteState);
982
983     return E_OK;
984 }
985
986 am_Error_e DatabaseHandler::changeMainSinkSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sinkID_t sinkID)
987 {
988     //todo: add checks if soundproperty exists!
989     assert(sinkID!=0);
990
991     sqlite3_stmt* query = NULL;
992     int eCode = 0;
993     std::string command;
994
995     if (!existSink(sinkID))
996     {
997         return E_NON_EXISTENT;
998     }
999     command = "UPDATE SinkMainSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1000     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1001     sqlite3_bind_int(query, 1, soundProperty.value);
1002     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1003     {
1004         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainSinkSoundPropertyDB SQLITE Step error code:"), DLT_INT(eCode));
1005         return E_DATABASE_ERROR;
1006     }assert(sinkID!=0);
1007
1008     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1009     {
1010         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainSinkSoundPropertyDB SQLITE Finalize error code:"), DLT_INT(eCode));
1011         return E_DATABASE_ERROR;
1012     }
1013
1014     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeMainSinkSoundPropertyDB changed MainSinkSoundProperty of sink:"), DLT_INT(sinkID), DLT_STRING("type:"), DLT_INT(soundProperty.type), DLT_STRING("to:"), DLT_INT(soundProperty.value));
1015     if (mDatabaseObserver)
1016         mDatabaseObserver->mainSinkSoundPropertyChanged(sinkID, soundProperty);
1017     return E_OK;
1018 }
1019
1020 am_Error_e DatabaseHandler::changeMainSourceSoundPropertyDB(const am_MainSoundProperty_s & soundProperty, const am_sourceID_t sourceID)
1021 {
1022     //todo: add checks if soundproperty exists!
1023     assert(sourceID!=0);
1024
1025     sqlite3_stmt* query = NULL;
1026     int eCode = 0;
1027     std::string command;
1028
1029     if (!existSource(sourceID))
1030     {
1031         return E_NON_EXISTENT;
1032     }
1033     command = "UPDATE SourceMainSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
1034     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1035     sqlite3_bind_int(query, 1, soundProperty.value);
1036     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1037     {
1038         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainSourceSoundPropertyDB SQLITE Step error code:"), DLT_INT(eCode));
1039         return E_DATABASE_ERROR;
1040     }
1041
1042     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1043     {
1044         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeMainSourceSoundPropertyDB SQLITE Finalize error code:"), DLT_INT(eCode));
1045         return E_DATABASE_ERROR;
1046     }
1047
1048     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeMainSourceSoundPropertyDB changed MainSinkSoundProperty of source:"), DLT_INT(sourceID), DLT_STRING("type:"), DLT_INT(soundProperty.type), DLT_STRING("to:"), DLT_INT(soundProperty.value));
1049
1050     if (mDatabaseObserver)
1051         mDatabaseObserver->mainSourceSoundPropertyChanged(sourceID, soundProperty);
1052     return E_OK;
1053 }
1054
1055 am_Error_e DatabaseHandler::changeSourceAvailabilityDB(const am_Availability_s & availability, const am_sourceID_t sourceID)
1056 {
1057     assert(sourceID!=0);
1058
1059     sqlite3_stmt* query = NULL;
1060     int eCode = 0;
1061     std::string command;
1062
1063     if (!existSource(sourceID))
1064     {
1065         return E_NON_EXISTENT;
1066     }
1067     command = "UPDATE " + std::string(SOURCE_TABLE) + " SET availability=?, availabilityReason=? WHERE sourceID=" + i2s(sourceID);
1068     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1069     sqlite3_bind_int(query, 1, availability.availability);
1070     sqlite3_bind_int(query, 2, availability.availabilityReason);
1071     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1072     {
1073         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceAvailabilityDB SQLITE Step error code:"), DLT_INT(eCode));
1074         return E_DATABASE_ERROR;
1075     }
1076
1077     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1078     {
1079         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceAvailabilityDB SQLITE Finalize error code:"), DLT_INT(eCode));
1080         return E_DATABASE_ERROR;
1081     }
1082
1083     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSourceAvailabilityDB changed changeSourceAvailabilityDB of source:"), DLT_INT(sourceID), DLT_STRING("to:"), DLT_INT(availability.availability), DLT_STRING("Reason:"), DLT_INT(availability.availabilityReason));
1084
1085     if (mDatabaseObserver && sourceVisible(sourceID))
1086         mDatabaseObserver->sourceAvailabilityChanged(sourceID, availability);
1087     return E_OK;
1088 }
1089
1090 am_Error_e DatabaseHandler::changeSystemPropertyDB(const am_SystemProperty_s & property)
1091 {
1092     sqlite3_stmt* query = NULL;
1093     int eCode = 0;
1094     std::string command = "UPDATE " + std::string(SYSTEM_TABLE) + " set value=? WHERE type=?";
1095
1096     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1097     sqlite3_bind_int(query, 1, property.value);
1098     sqlite3_bind_int(query, 2, property.type);
1099
1100     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1101     {
1102         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSystemPropertyDB SQLITE Step error code:"), DLT_INT(eCode));
1103         return E_DATABASE_ERROR;
1104     }
1105
1106     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1107     {
1108         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSystemPropertyDB SQLITE Finalize error code:"), DLT_INT(eCode));
1109         return E_DATABASE_ERROR;
1110     }
1111
1112     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSystemPropertyDB changed system property"));
1113
1114     if (mDatabaseObserver)
1115         mDatabaseObserver->systemPropertyChanged(property);
1116
1117     return E_OK;
1118 }
1119
1120 am_Error_e DatabaseHandler::removeMainConnectionDB(const am_mainConnectionID_t mainConnectionID)
1121 {
1122     assert(mainConnectionID!=0);
1123
1124     if (!existMainConnection(mainConnectionID))
1125     {
1126         return E_NON_EXISTENT;
1127     }
1128     std::string command = "DELETE from " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
1129     std::string command1 = "DROP table MainConnectionRoute" + i2s(mainConnectionID);
1130     if (!sqQuery(command))
1131         return E_DATABASE_ERROR;
1132     if (!sqQuery(command1))
1133         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeMainConnectionDB removed:"), DLT_INT(mainConnectionID));
1134     if (mDatabaseObserver)
1135     {
1136         mDatabaseObserver->mainConnectionStateChanged(mainConnectionID, CS_DISCONNECTED);
1137         mDatabaseObserver->numberOfMainConnectionsChanged();
1138     }
1139     return E_OK;
1140 }
1141
1142 am_Error_e DatabaseHandler::removeSinkDB(const am_sinkID_t sinkID)
1143 {
1144     assert(sinkID!=0);
1145
1146     if (!existSink(sinkID))
1147     {
1148         return E_NON_EXISTENT;
1149     }
1150     std::string command = "DELETE from " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
1151     std::string command1 = "DROP table SinkConnectionFormat" + i2s(sinkID);
1152     std::string command2 = "DROP table SinkMainSoundProperty" + i2s(sinkID);
1153     std::string command3 = "DROP table SinkSoundProperty" + i2s(sinkID);
1154     if (!sqQuery(command))
1155         return E_DATABASE_ERROR;
1156     if (!sqQuery(command1))
1157         return E_DATABASE_ERROR;
1158     if (!sqQuery(command2))
1159         return E_DATABASE_ERROR;
1160     if (!sqQuery(command3))
1161         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeSinkDB removed:"), DLT_INT(sinkID));
1162
1163     if (mDatabaseObserver != NULL)
1164         mDatabaseObserver->removedSink(sinkID);
1165
1166     return E_OK;
1167 }
1168
1169 am_Error_e DatabaseHandler::removeSourceDB(const am_sourceID_t sourceID)
1170 {
1171     assert(sourceID!=0);
1172
1173     if (!existSource(sourceID))
1174     {
1175         return E_NON_EXISTENT;
1176     }
1177     std::string command = "DELETE from " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
1178     std::string command1 = "DROP table SourceConnectionFormat" + i2s(sourceID);
1179     std::string command2 = "DROP table SourceMainSoundProperty" + i2s(sourceID);
1180     std::string command3 = "DROP table SourceSoundProperty" + i2s(sourceID);
1181     if (!sqQuery(command))
1182         return E_DATABASE_ERROR;
1183     if (!sqQuery(command1))
1184         return E_DATABASE_ERROR;
1185     if (!sqQuery(command2))
1186         return E_DATABASE_ERROR;
1187     if (!sqQuery(command3))
1188         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeSourceDB removed:"), DLT_INT(sourceID));
1189     if (mDatabaseObserver)
1190         mDatabaseObserver->removedSource(sourceID);
1191     return E_OK;
1192 }
1193
1194 am_Error_e DatabaseHandler::removeGatewayDB(const am_gatewayID_t gatewayID)
1195 {
1196     assert(gatewayID!=0);
1197
1198     if (!existGateway(gatewayID))
1199     {
1200         return E_NON_EXISTENT;
1201     }
1202     std::string command = "DELETE from " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1203     if (!sqQuery(command))
1204         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeGatewayDB removed:"), DLT_INT(gatewayID));
1205     if (mDatabaseObserver)
1206         mDatabaseObserver->removeGateway(gatewayID);
1207     return E_OK;
1208 }
1209
1210 am_Error_e DatabaseHandler::removeCrossfaderDB(const am_crossfaderID_t crossfaderID)
1211 {
1212     //todo: implement crossdfader
1213     (void) crossfaderID;
1214     return E_UNKNOWN;
1215 }
1216
1217 am_Error_e DatabaseHandler::removeDomainDB(const am_domainID_t domainID)
1218 {
1219     assert(domainID!=0);
1220
1221     if (!existDomain(domainID))
1222     {
1223         return E_NON_EXISTENT;
1224     }
1225     std::string command = "DELETE from " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
1226     if (!sqQuery(command))
1227         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeDomainDB removed:"), DLT_INT(domainID));
1228     if (mDatabaseObserver)
1229         mDatabaseObserver->removeDomain(domainID);
1230     return E_OK;
1231 }
1232
1233 am_Error_e DatabaseHandler::removeSinkClassDB(const am_sinkClass_t sinkClassID)
1234 {
1235     assert(sinkClassID!=0);
1236
1237     if (!existSinkClass(sinkClassID))
1238     {
1239         return E_NON_EXISTENT;
1240     }
1241     std::string command = "DELETE from " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
1242     std::string command1 = "DROP table SinkClassProperties" + i2s(sinkClassID);
1243     if (!sqQuery(command))
1244         return E_DATABASE_ERROR;
1245     if (!sqQuery(command1))
1246         return E_DATABASE_ERROR;
1247
1248     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeSinkClassDB removed:"), DLT_INT(sinkClassID));
1249     if (mDatabaseObserver)
1250         mDatabaseObserver->numberOfSinkClassesChanged();
1251
1252     return E_OK;
1253 }
1254
1255 am_Error_e DatabaseHandler::removeSourceClassDB(const am_sourceClass_t sourceClassID)
1256 {
1257     assert(sourceClassID!=0);
1258
1259     if (!existSourceClass(sourceClassID))
1260     {
1261         return E_NON_EXISTENT;
1262     }
1263     std::string command = "DELETE from " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
1264     std::string command1 = "DROP table SourceClassProperties" + i2s(sourceClassID);
1265     if (!sqQuery(command))
1266         return E_DATABASE_ERROR;
1267     if (!sqQuery(command1))
1268         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeSourceClassDB removed:"), DLT_INT(sourceClassID));
1269
1270     if (mDatabaseObserver)
1271         mDatabaseObserver->numberOfSourceClassesChanged();
1272     return E_OK;
1273 }
1274
1275 am_Error_e DatabaseHandler::removeConnection(const am_connectionID_t connectionID)
1276 {
1277     assert(connectionID!=0);
1278
1279     std::string command = "DELETE from " + std::string(CONNECTION_TABLE) + " WHERE connectionID=" + i2s(connectionID);
1280     std::string command1 = "DROP table SourceClassProperties" + i2s(connectionID);
1281     if (!sqQuery(command))
1282         return E_DATABASE_ERROR;
1283     if (!sqQuery(command1))
1284         return E_DATABASE_ERROR;DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::removeConnection removed:"), DLT_INT(connectionID));
1285
1286     return E_OK;
1287 }
1288
1289 am_Error_e DatabaseHandler::getSourceClassInfoDB(const am_sourceID_t sourceID, am_SourceClass_s & classInfo) const
1290 {
1291     assert(sourceID!=0);
1292
1293     if (!existSource(sourceID))
1294     {
1295         return E_NON_EXISTENT;
1296     }
1297     sqlite3_stmt* query = NULL;
1298     int eCode = 0;
1299     am_ClassProperty_s propertyTemp;
1300     std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + (i2s(sourceID));
1301     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1302
1303     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1304     {
1305         classInfo.sourceClassID = sqlite3_column_int(query, 0);
1306     }
1307
1308     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1309     {
1310         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1311         return E_DATABASE_ERROR;
1312     }
1313
1314     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1315     {
1316         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1317         return E_DATABASE_ERROR;
1318     }
1319
1320     command = "SELECT name FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + (i2s(classInfo.sourceClassID));
1321     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1322
1323     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1324     {
1325         classInfo.name = std::string((const char*) sqlite3_column_text(query, 0));
1326     }
1327
1328     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1329     {
1330         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1331         return E_DATABASE_ERROR;
1332     }
1333
1334     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1335     {
1336         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1337         return E_DATABASE_ERROR;
1338     }
1339
1340     //read out Properties
1341     command = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classInfo.sourceClassID);
1342     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1343     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1344     {
1345         propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1346         propertyTemp.value = sqlite3_column_int(query, 1);
1347         classInfo.listClassProperties.push_back(propertyTemp);
1348     }
1349
1350     if (eCode != SQLITE_DONE)
1351     {
1352         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1353         return E_DATABASE_ERROR;
1354     }
1355
1356     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1357     {
1358         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1359         return E_DATABASE_ERROR;
1360     }
1361     return E_OK;
1362 }
1363
1364 am_Error_e DatabaseHandler::changeSinkClassInfoDB(const am_SinkClass_s& sinkClass)
1365 {
1366     assert(sinkClass.sinkClassID!=0);
1367     assert(!sinkClass.listClassProperties.empty());
1368
1369     sqlite3_stmt* query = NULL;
1370     int eCode = 0;
1371
1372     //check if the ID already exists
1373     if (!existSinkClass(sinkClass.sinkClassID))
1374         return E_NON_EXISTENT;
1375
1376     //fill ConnectionFormats
1377     std::string command = "UPDATE SinkClassProperties" + i2s(sinkClass.sinkClassID) + " set value=? WHERE classProperty=?;";
1378     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1379     std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
1380     for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
1381     {
1382         sqlite3_bind_int(query, 1, Iterator->value);
1383         sqlite3_bind_int(query, 2, Iterator->classProperty);
1384         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1385         {
1386             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:"), DLT_INT(eCode));
1387             return E_DATABASE_ERROR;
1388         }
1389         sqlite3_reset(query);
1390     }
1391
1392     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1393     {
1394         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::setSinkClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1395         return E_DATABASE_ERROR;
1396     }
1397
1398     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo"));
1399     return E_OK;
1400 }
1401
1402 am_Error_e DatabaseHandler::changeSourceClassInfoDB(const am_SourceClass_s& sourceClass)
1403 {
1404     assert(sourceClass.sourceClassID!=0);
1405     assert(!sourceClass.listClassProperties.empty());
1406
1407     sqlite3_stmt* query = NULL;
1408     int eCode = 0;
1409
1410     //check if the ID already exists
1411     if (!existSourceClass(sourceClass.sourceClassID))
1412         return E_NON_EXISTENT;
1413
1414     //fill ConnectionFormats
1415     std::string command = "UPDATE SourceClassProperties" + i2s(sourceClass.sourceClassID) + " set value=? WHERE classProperty=?;";
1416     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1417     std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
1418     for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
1419     {
1420         sqlite3_bind_int(query, 1, Iterator->value);
1421         sqlite3_bind_int(query, 2, Iterator->classProperty);
1422         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1423         {
1424             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::setSinkClassInfoDB SQLITE Step error code:"), DLT_INT(eCode));
1425             return E_DATABASE_ERROR;
1426         }
1427         sqlite3_reset(query);
1428     }
1429
1430     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1431     {
1432         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::setSinkClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1433         return E_DATABASE_ERROR;
1434     }
1435
1436     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::setSinkClassInfoDB set setSinkClassInfo"));
1437     return E_OK;
1438 }
1439
1440 am_Error_e DatabaseHandler::getSinkClassInfoDB(const am_sinkID_t sinkID, am_SinkClass_s & sinkClass) const
1441 {
1442     assert(sinkID!=0);
1443
1444     if (!existSink(sinkID))
1445     {
1446         return E_NON_EXISTENT;
1447     }
1448     sqlite3_stmt* query = NULL;
1449     int eCode = 0;
1450     am_ClassProperty_s propertyTemp;
1451     std::string command = "SELECT sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + (i2s(sinkID));
1452     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1453
1454     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1455     {
1456         sinkClass.sinkClassID = sqlite3_column_int(query, 0);
1457     }
1458
1459     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1460     {
1461         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1462         return E_DATABASE_ERROR;
1463     }
1464
1465     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1466     {
1467         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1468         return E_DATABASE_ERROR;
1469     }
1470
1471     command = "SELECT name FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + (i2s(sinkClass.sinkClassID));
1472     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1473
1474     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1475     {
1476         sinkClass.name = std::string((const char*) sqlite3_column_text(query, 0));
1477     }
1478
1479     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
1480     {
1481         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1482         return E_DATABASE_ERROR;
1483     }
1484
1485     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1486     {
1487         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1488         return E_DATABASE_ERROR;
1489     }
1490
1491     //read out Properties
1492     command = "SELECT classProperty, value FROM SinkClassProperties" + i2s(sinkClass.sinkClassID);
1493     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1494     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1495     {
1496         propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(query, 0);
1497         propertyTemp.value = sqlite3_column_int(query, 1);
1498         sinkClass.listClassProperties.push_back(propertyTemp);
1499     }
1500
1501     if (eCode != SQLITE_DONE)
1502     {
1503         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE error code:"), DLT_INT(eCode));
1504         return E_DATABASE_ERROR;
1505     }
1506
1507     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1508     {
1509         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkClassInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1510         return E_DATABASE_ERROR;
1511     }
1512     return E_OK;
1513 }
1514
1515 am_Error_e DatabaseHandler::getGatewayInfoDB(const am_gatewayID_t gatewayID, am_Gateway_s & gatewayData) const
1516 {
1517     assert(gatewayID!=0);
1518     if (!existGateway(gatewayID))
1519     {
1520         return E_NON_EXISTENT;
1521     }
1522     sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
1523     int eCode = 0;
1524     am_ConnectionFormat_e tempConnectionFormat;
1525     std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
1526     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1527
1528     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1529     {
1530         gatewayData.name = std::string((const char*) sqlite3_column_text(query, 0));
1531         gatewayData.sinkID = sqlite3_column_int(query, 1);
1532         gatewayData.sourceID = sqlite3_column_int(query, 2);
1533         gatewayData.domainSinkID = sqlite3_column_int(query, 3);
1534         gatewayData.domainSourceID = sqlite3_column_int(query, 4);
1535         gatewayData.controlDomainID = sqlite3_column_int(query, 5);
1536         gatewayData.gatewayID = sqlite3_column_int(query, 6);
1537
1538         //convertionMatrix:
1539         ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
1540         iter = mListConnectionFormat.find(gatewayData.gatewayID);
1541         if (iter == mListConnectionFormat.end())
1542         {
1543             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getGatewayInfoDB database error with convertionFormat"));
1544             return E_DATABASE_ERROR;
1545         }
1546         gatewayData.convertionMatrix = iter->second;
1547
1548         //read out the connectionFormats
1549         std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(gatewayData.gatewayID);
1550         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL);
1551         while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
1552         {
1553             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
1554             gatewayData.listSourceFormats.push_back(tempConnectionFormat);
1555         }
1556
1557         if ((eCode = sqlite3_finalize(qSourceConnectionFormat)) != SQLITE_OK)
1558         {
1559             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1560             return E_DATABASE_ERROR;
1561         }
1562
1563         //read out sound properties
1564         commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(gatewayData.gatewayID);
1565         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL);
1566         while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
1567         {
1568             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
1569             gatewayData.listSinkFormats.push_back(tempConnectionFormat);
1570         }
1571
1572         if ((eCode = sqlite3_finalize(qSinkConnectionFormat)) != SQLITE_OK)
1573         {
1574             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1575             return E_DATABASE_ERROR;
1576         }
1577
1578     }
1579
1580     if (eCode != SQLITE_DONE)
1581     {
1582         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getGatewayInfoDB SQLITE error code:"), DLT_INT(eCode));
1583         return E_DATABASE_ERROR;
1584     }
1585
1586     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1587     {
1588         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getGatewayInfoDB SQLITE Finalize error code:"), DLT_INT(eCode));
1589         return E_DATABASE_ERROR;
1590     }
1591
1592     return E_OK;
1593
1594 }
1595
1596 am_Error_e DatabaseHandler::getCrossfaderInfoDB(const am_crossfaderID_t crossfaderID, am_Crossfader_s & crossfaderData) const
1597 {
1598     //todo: implement crossfader
1599     (void) crossfaderID;
1600     (void) crossfaderData;
1601     return E_UNKNOWN;
1602 }
1603
1604 am_Error_e DatabaseHandler::getListSinksOfDomain(const am_domainID_t domainID, std::vector<am_sinkID_t> & listSinkID) const
1605 {
1606     assert(domainID!=0);
1607     listSinkID.clear();
1608     if (!existDomain(domainID))
1609     {
1610         return E_NON_EXISTENT;
1611     }
1612     sqlite3_stmt* query = NULL;
1613     int eCode = 0;
1614     am_sinkID_t temp;
1615     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND domainID=" + (i2s(domainID));
1616     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1617
1618     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1619     {
1620         temp = sqlite3_column_int(query, 0);
1621         listSinkID.push_back(temp);
1622     }
1623
1624     if (eCode != SQLITE_DONE)
1625     {
1626         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinksOfDomain SQLITE error code:"), DLT_INT(eCode));
1627         return E_DATABASE_ERROR;
1628     }
1629
1630     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1631     {
1632         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinksOfDomain SQLITE Finalize error code:"), DLT_INT(eCode));
1633         return E_DATABASE_ERROR;
1634     }
1635
1636     return E_OK;
1637 }
1638
1639 am_Error_e DatabaseHandler::getListSourcesOfDomain(const am_domainID_t domainID, std::vector<am_sourceID_t> & listSourceID) const
1640 {
1641     assert(domainID!=0);
1642     listSourceID.clear();
1643     if (!existDomain(domainID))
1644     {
1645         return E_NON_EXISTENT;
1646     }
1647     sqlite3_stmt* query = NULL;
1648     int eCode = 0;
1649     am_sourceID_t temp;
1650     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
1651
1652     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1653
1654     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1655     {
1656         temp = sqlite3_column_int(query, 0);
1657         listSourceID.push_back(temp);
1658     }
1659
1660     if (eCode != SQLITE_DONE)
1661     {
1662         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourcesOfDomain SQLITE error code:"), DLT_INT(eCode));
1663         return E_DATABASE_ERROR;
1664     }
1665
1666     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1667     {
1668         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourcesOfDomain SQLITE Finalize error code:"), DLT_INT(eCode));
1669         return E_DATABASE_ERROR;
1670     }
1671
1672     return E_OK;
1673 }
1674
1675 am_Error_e DatabaseHandler::getListCrossfadersOfDomain(const am_domainID_t domainID, std::vector<am_crossfaderID_t> & listGatewaysID) const
1676 {
1677     //todo: implement crossfader
1678     (void) listGatewaysID;
1679     (void) domainID;
1680     return E_UNKNOWN;
1681
1682 }
1683
1684 am_Error_e DatabaseHandler::getListGatewaysOfDomain(const am_domainID_t domainID, std::vector<am_gatewayID_t> & listGatewaysID) const
1685 {
1686     assert(domainID!=0);
1687     listGatewaysID.clear();
1688     if (!existDomain(domainID))
1689     {
1690         return E_NON_EXISTENT;
1691     }
1692     sqlite3_stmt* query = NULL;
1693     int eCode = 0;
1694     am_gatewayID_t temp;
1695
1696     std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE controlDomainID=" + i2s(domainID);
1697     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1698
1699     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1700     {
1701         temp = sqlite3_column_int(query, 0);
1702         listGatewaysID.push_back(temp);
1703     }
1704
1705     if (eCode != SQLITE_DONE)
1706     {
1707         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGatewaysOfDomain SQLITE error code:"), DLT_INT(eCode));
1708         return E_DATABASE_ERROR;
1709     }
1710
1711     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1712     {
1713         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGatewaysOfDomain SQLITE Finalize error code:"), DLT_INT(eCode));
1714         return E_DATABASE_ERROR;
1715     }
1716
1717     return E_OK;
1718 }
1719
1720 am_Error_e DatabaseHandler::getListMainConnections(std::vector<am_MainConnection_s> & listMainConnections) const
1721 {
1722     listMainConnections.clear();
1723     sqlite3_stmt *query = NULL, *query1 = NULL, *query2 = NULL;
1724     int eCode = 0;
1725     am_MainConnection_s temp;
1726     am_RoutingElement_s tempRoute;
1727
1728     std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
1729     std::string command1 = "SELECT connectionID FROM MainConnectionRoute";
1730     std::string command2 = "SELECT sourceID, sinkID, connectionFormat FROM " + std::string(CONNECTION_TABLE) + " WHERE connectionID=?";
1731     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1732     sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &query2, NULL);
1733
1734     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1735     {
1736         temp.connectionID = sqlite3_column_int(query, 0);
1737         temp.route.sourceID = sqlite3_column_int(query, 1);
1738         temp.route.sinkID = sqlite3_column_int(query, 2);
1739         temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
1740         temp.delay = sqlite3_column_int(query, 4);
1741         std::string statement = command1 + i2s(temp.connectionID);
1742         sqlite3_prepare_v2(mDatabase, statement.c_str(), -1, &query1, NULL);
1743         while ((eCode = sqlite3_step(query1)) == SQLITE_ROW) //todo: check results of eCode1, eCode2
1744         {
1745             int k = sqlite3_column_int(query1, 0);
1746             sqlite3_bind_int(query2, 1, k);
1747             while ((eCode = sqlite3_step(query2)) == SQLITE_ROW)
1748             {
1749                 tempRoute.sourceID = sqlite3_column_int(query2, 0);
1750                 tempRoute.sinkID = sqlite3_column_int(query2, 1);
1751                 tempRoute.connectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(query2, 2);
1752                 getDomainOfSource(tempRoute.sourceID, tempRoute.domainID);
1753                 temp.route.route.push_back(tempRoute);
1754             }
1755             sqlite3_reset(query2);
1756         }
1757         listMainConnections.push_back(temp);
1758     }
1759
1760     if (eCode != SQLITE_DONE)
1761     {
1762         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainConnections SQLITE error code:"), DLT_INT(eCode));
1763         return E_DATABASE_ERROR;
1764     }
1765
1766     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1767     {
1768         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainConnections SQLITE Finalize error code:"), DLT_INT(eCode));
1769         return E_DATABASE_ERROR;
1770     }
1771
1772     return E_OK;
1773 }
1774
1775 am_Error_e DatabaseHandler::getListDomains(std::vector<am_Domain_s> & listDomains) const
1776 {
1777     listDomains.clear();
1778     sqlite3_stmt* query = NULL;
1779     int eCode = 0;
1780     am_Domain_s temp;
1781     std::string command = "SELECT domainID, name, busname, nodename, early, complete, state FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0";
1782     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1783
1784     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1785     {
1786         temp.domainID = sqlite3_column_int(query, 0);
1787         temp.name = std::string((const char*) sqlite3_column_text(query, 1));
1788         temp.busname = std::string((const char*) sqlite3_column_text(query, 2));
1789         temp.nodename = std::string((const char*) sqlite3_column_text(query, 3));
1790         temp.early = sqlite3_column_int(query, 4);
1791         temp.complete = sqlite3_column_int(query, 5);
1792         temp.state = (am_DomainState_e) sqlite3_column_int(query, 6);
1793         listDomains.push_back(temp);
1794     }
1795
1796     if (eCode != SQLITE_DONE)
1797     {
1798         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListDomains SQLITE error code:"), DLT_INT(eCode));
1799         return E_DATABASE_ERROR;
1800     }
1801
1802     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1803     {
1804         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListDomains SQLITE Finalize error code:"), DLT_INT(eCode));
1805         return E_DATABASE_ERROR;
1806     }
1807
1808     return E_OK;
1809 }
1810
1811 am_Error_e DatabaseHandler::getListConnections(std::vector<am_Connection_s> & listConnections) const
1812 {
1813     listConnections.clear();
1814     sqlite3_stmt* query = NULL;
1815     int eCode = 0;
1816     am_Connection_s temp;
1817     std::string command = "SELECT connectionID, sourceID, sinkID, delay, connectionFormat FROM " + std::string(CONNECTION_TABLE) + " WHERE reserved=0";
1818     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1819
1820     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1821     {
1822         temp.connectionID = sqlite3_column_int(query, 0);
1823         temp.sourceID = sqlite3_column_int(query, 1);
1824         temp.sinkID = sqlite3_column_int(query, 2);
1825         temp.delay = sqlite3_column_int(query, 3);
1826         temp.connectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(query, 4);
1827         listConnections.push_back(temp);
1828     }
1829
1830     if (eCode != SQLITE_DONE)
1831     {
1832         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListConnections SQLITE error code:"), DLT_INT(eCode));
1833         return E_DATABASE_ERROR;
1834     }
1835
1836     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1837     {
1838         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListConnections SQLITE Finalize error code:"), DLT_INT(eCode));
1839         return E_DATABASE_ERROR;
1840     }
1841
1842     return E_OK;
1843 }
1844
1845 am_Error_e DatabaseHandler::getListSinks(std::vector<am_Sink_s> & listSinks) const
1846 {
1847     listSinks.clear();
1848     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL;
1849     int eCode = 0;
1850     am_Sink_s temp;
1851     am_ConnectionFormat_e tempConnectionFormat;
1852     am_SoundProperty_s tempSoundProperty;
1853     am_MainSoundProperty_s tempMainSoundProperty;
1854     std::string command = "SELECT name, domainID, sinkClassID, volume, visible, availability, availabilityReason, muteState, mainVolume, sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0";
1855     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1856
1857     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1858     {
1859         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
1860         temp.domainID = sqlite3_column_int(query, 1);
1861         temp.sinkClassID = sqlite3_column_int(query, 2);
1862         temp.volume = sqlite3_column_int(query, 3);
1863         temp.visible = sqlite3_column_int(query, 4);
1864         temp.available.availability = (am_Availablility_e) sqlite3_column_int(query, 5);
1865         temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 6);
1866         temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 7);
1867         temp.mainVolume = sqlite3_column_int(query, 8);
1868         temp.sinkID = sqlite3_column_int(query, 9);
1869
1870         //read out the connectionFormats
1871         std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(temp.sinkID);
1872         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
1873         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1874         {
1875             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1876             temp.listConnectionFormats.push_back(tempConnectionFormat);
1877         }
1878
1879         if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
1880         {
1881             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE Finalize error code:"), DLT_INT(eCode));
1882             return E_DATABASE_ERROR;
1883         }
1884
1885         //read out sound properties
1886         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SinkSoundProperty" + i2s(temp.sinkID);
1887         sqlite3_prepare_v2(mDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL);
1888         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1889         {
1890             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1891             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1892             temp.listSoundProperties.push_back(tempSoundProperty);
1893         }
1894
1895         if ((eCode = sqlite3_finalize(qSoundProperty)) != SQLITE_OK)
1896         {
1897             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE Finalize error code:"), DLT_INT(eCode));
1898             return E_DATABASE_ERROR;
1899         }
1900
1901         //read out MainSoundProperties
1902         std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(temp.sinkID);
1903         sqlite3_prepare_v2(mDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL);
1904         while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
1905         {
1906             tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
1907             tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
1908             temp.listMainSoundProperties.push_back(tempMainSoundProperty);
1909         }
1910
1911         if ((eCode = sqlite3_finalize(qMAinSoundProperty)) != SQLITE_OK)
1912         {
1913             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE Finalize error code:"), DLT_INT(eCode));
1914             return E_DATABASE_ERROR;
1915         }
1916         listSinks.push_back(temp);
1917         temp.listConnectionFormats.clear();
1918         temp.listMainSoundProperties.clear();
1919         temp.listSoundProperties.clear();
1920     }
1921
1922     if (eCode != SQLITE_DONE)
1923     {
1924         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE error code:"), DLT_INT(eCode));
1925         return E_DATABASE_ERROR;
1926     }
1927
1928     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
1929     {
1930         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE Finalize error code:"), DLT_INT(eCode));
1931         return E_DATABASE_ERROR;
1932     }
1933
1934     return E_OK;
1935 }
1936
1937 am_Error_e DatabaseHandler::getListSources(std::vector<am_Source_s> & listSources) const
1938 {
1939     listSources.clear();
1940     sqlite3_stmt* query = NULL, *qConnectionFormat = NULL, *qSoundProperty = NULL, *qMAinSoundProperty = NULL;
1941     int eCode = 0;
1942     am_Source_s temp;
1943     am_ConnectionFormat_e tempConnectionFormat;
1944     am_SoundProperty_s tempSoundProperty;
1945     am_MainSoundProperty_s tempMainSoundProperty;
1946     std::string command = "SELECT name, domainID, sourceClassID, sourceState, volume, visible, availability, availabilityReason, interruptState, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0";
1947     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
1948
1949     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
1950     {
1951         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
1952         temp.domainID = sqlite3_column_int(query, 1);
1953         temp.sourceClassID = sqlite3_column_int(query, 2);
1954         temp.sourceState = (am_SourceState_e) sqlite3_column_int(query, 3);
1955         temp.volume = sqlite3_column_int(query, 4);
1956         temp.visible = sqlite3_column_int(query, 5);
1957         temp.available.availability = (am_Availablility_e) sqlite3_column_int(query, 6);
1958         temp.available.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 7);
1959         temp.interruptState = (am_InterruptState_e) sqlite3_column_int(query, 8);
1960         temp.sourceID = sqlite3_column_int(query, 9);
1961
1962         //read out the connectionFormats
1963         std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(temp.sourceID);
1964         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
1965         while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
1966         {
1967             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
1968             temp.listConnectionFormats.push_back(tempConnectionFormat);
1969         }
1970
1971         if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
1972         {
1973             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
1974             return E_DATABASE_ERROR;
1975         }
1976
1977         //read out sound properties
1978         std::string commandSoundProperty = "SELECT soundPropertyType, value FROM SourceSoundProperty" + i2s(temp.sourceID);
1979         sqlite3_prepare_v2(mDatabase, commandSoundProperty.c_str(), -1, &qSoundProperty, NULL);
1980         while ((eCode = sqlite3_step(qSoundProperty)) == SQLITE_ROW)
1981         {
1982             tempSoundProperty.type = (am_SoundPropertyType_e) sqlite3_column_int(qSoundProperty, 0);
1983             tempSoundProperty.value = sqlite3_column_int(qSoundProperty, 1);
1984             temp.listSoundProperties.push_back(tempSoundProperty);
1985         }
1986
1987         if ((eCode = sqlite3_finalize(qSoundProperty)) != SQLITE_OK)
1988         {
1989             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
1990             return E_DATABASE_ERROR;
1991         }
1992
1993         //read out MainSoundProperties
1994         std::string commandMainSoundProperty = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(temp.sourceID);
1995         sqlite3_prepare_v2(mDatabase, commandMainSoundProperty.c_str(), -1, &qMAinSoundProperty, NULL);
1996         while ((eCode = sqlite3_step(qMAinSoundProperty)) == SQLITE_ROW)
1997         {
1998             tempMainSoundProperty.type = (am_MainSoundPropertyType_e) sqlite3_column_int(qMAinSoundProperty, 0);
1999             tempMainSoundProperty.value = sqlite3_column_int(qMAinSoundProperty, 1);
2000             temp.listMainSoundProperties.push_back(tempMainSoundProperty);
2001         }
2002
2003         if ((eCode = sqlite3_finalize(qMAinSoundProperty)) != SQLITE_OK)
2004         {
2005             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
2006             return E_DATABASE_ERROR;
2007         }
2008         listSources.push_back(temp);
2009         temp.listConnectionFormats.clear();
2010         temp.listMainSoundProperties.clear();
2011         temp.listSoundProperties.clear();
2012     }
2013
2014     if (eCode != SQLITE_DONE)
2015     {
2016         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE error code:"), DLT_INT(eCode));
2017         return E_DATABASE_ERROR;
2018     }
2019
2020     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2021     {
2022         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
2023         return E_DATABASE_ERROR;
2024     }
2025
2026     return E_OK;
2027 }
2028
2029 am_Error_e DatabaseHandler::getListSourceClasses(std::vector<am_SourceClass_s> & listSourceClasses) const
2030 {
2031     listSourceClasses.clear();
2032
2033     sqlite3_stmt* query = NULL, *subQuery = NULL;
2034     int eCode = 0, eCode1;
2035     am_SourceClass_s classTemp;
2036     am_ClassProperty_s propertyTemp;
2037
2038     std::string command = "SELECT sourceClassID, name FROM " + std::string(SOURCE_CLASS_TABLE);
2039     std::string command2;
2040     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2041
2042     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2043     {
2044         classTemp.sourceClassID = sqlite3_column_int(query, 0);
2045         classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2046
2047         //read out Properties
2048         command2 = "SELECT classProperty, value FROM SourceClassProperties" + i2s(classTemp.sourceClassID);
2049         sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &subQuery, NULL);
2050
2051         while ((eCode1 = sqlite3_step(subQuery)) == SQLITE_ROW)
2052         {
2053             propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2054             propertyTemp.value = sqlite3_column_int(subQuery, 1);
2055             classTemp.listClassProperties.push_back(propertyTemp);
2056         }
2057
2058         if (eCode1 != SQLITE_DONE)
2059         {
2060             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE error code:"), DLT_INT(eCode1));
2061             return E_DATABASE_ERROR;
2062         }
2063
2064         if ((eCode1 = sqlite3_finalize(subQuery)) != SQLITE_OK)
2065         {
2066             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:"), DLT_INT(eCode1));
2067             return E_DATABASE_ERROR;
2068         }
2069         listSourceClasses.push_back(classTemp);
2070     }
2071
2072     if (eCode != SQLITE_DONE)
2073     {
2074         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE error code:"), DLT_INT(eCode));
2075         return E_DATABASE_ERROR;
2076     }
2077
2078     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2079     {
2080         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:"), DLT_INT(eCode));
2081         return E_DATABASE_ERROR;
2082     }
2083
2084     return E_OK;
2085 }
2086
2087 am_Error_e DatabaseHandler::getListCrossfaders(std::vector<am_Crossfader_s> & listCrossfaders) const
2088 {
2089     //todo: implement crossfaders
2090     (void) listCrossfaders;
2091     return E_UNKNOWN;
2092 }
2093
2094 am_Error_e DatabaseHandler::getListGateways(std::vector<am_Gateway_s> & listGateways) const
2095 {
2096     listGateways.clear();
2097     sqlite3_stmt* query = NULL, *qSinkConnectionFormat = NULL, *qSourceConnectionFormat = NULL;
2098     int eCode = 0;
2099     am_Gateway_s temp;
2100     am_ConnectionFormat_e tempConnectionFormat;
2101
2102     std::string command = "SELECT name, sinkID, sourceID, domainSinkID, domainSourceID, controlDomainID, gatewayID FROM " + std::string(GATEWAY_TABLE);
2103     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2104
2105     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2106     {
2107         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2108         temp.sinkID = sqlite3_column_int(query, 1);
2109         temp.sourceID = sqlite3_column_int(query, 2);
2110         temp.domainSinkID = sqlite3_column_int(query, 3);
2111         temp.domainSourceID = sqlite3_column_int(query, 4);
2112         temp.controlDomainID = sqlite3_column_int(query, 5);
2113         temp.gatewayID = sqlite3_column_int(query, 6);
2114
2115         //convertionMatrix:
2116         ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2117         iter = mListConnectionFormat.find(temp.gatewayID);
2118         if (iter == mListConnectionFormat.end())
2119         {
2120             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGateways database error with convertionFormat"));
2121             return E_DATABASE_ERROR;
2122         }
2123         temp.convertionMatrix = iter->second;
2124
2125         //read out the connectionFormats
2126         std::string commandConnectionFormat = "SELECT soundFormat FROM GatewaySourceFormat" + i2s(temp.gatewayID);
2127         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSourceConnectionFormat, NULL);
2128         while ((eCode = sqlite3_step(qSourceConnectionFormat)) == SQLITE_ROW)
2129         {
2130             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSourceConnectionFormat, 0);
2131             temp.listSourceFormats.push_back(tempConnectionFormat);
2132         }
2133
2134         if ((eCode = sqlite3_finalize(qSourceConnectionFormat)) != SQLITE_OK)
2135         {
2136             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGateways SQLITE Finalize error code:"), DLT_INT(eCode));
2137             return E_DATABASE_ERROR;
2138         }
2139
2140         //read out sound properties
2141         commandConnectionFormat = "SELECT soundFormat FROM GatewaySinkFormat" + i2s(temp.gatewayID);
2142         sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qSinkConnectionFormat, NULL);
2143         while ((eCode = sqlite3_step(qSinkConnectionFormat)) == SQLITE_ROW)
2144         {
2145             tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qSinkConnectionFormat, 0);
2146             temp.listSinkFormats.push_back(tempConnectionFormat);
2147         }
2148
2149         if ((eCode = sqlite3_finalize(qSinkConnectionFormat)) != SQLITE_OK)
2150         {
2151             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGateways SQLITE Finalize error code:"), DLT_INT(eCode));
2152             return E_DATABASE_ERROR;
2153         }
2154
2155         listGateways.push_back(temp);
2156         temp.listSinkFormats.clear();
2157         temp.listSourceFormats.clear();
2158     }
2159
2160     if (eCode != SQLITE_DONE)
2161     {
2162         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGateways SQLITE error code:"), DLT_INT(eCode));
2163         return E_DATABASE_ERROR;
2164     }
2165
2166     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2167     {
2168         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGateways SQLITE Finalize error code:"), DLT_INT(eCode));
2169         return E_DATABASE_ERROR;
2170     }
2171
2172     return E_OK;
2173 }
2174
2175 am_Error_e DatabaseHandler::getListSinkClasses(std::vector<am_SinkClass_s> & listSinkClasses) const
2176 {
2177     listSinkClasses.clear();
2178
2179     sqlite3_stmt* query = NULL, *subQuery = NULL;
2180     int eCode = 0;
2181     am_SinkClass_s classTemp;
2182     am_ClassProperty_s propertyTemp;
2183
2184     std::string command = "SELECT sinkClassID, name FROM " + std::string(SINK_CLASS_TABLE);
2185     std::string command2;
2186     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2187
2188     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2189     {
2190         classTemp.sinkClassID = sqlite3_column_int(query, 0);
2191         classTemp.name = std::string((const char*) sqlite3_column_text(query, 1));
2192
2193         //read out Properties
2194         command2 = "SELECT classProperty, value FROM SinkClassProperties" + i2s(classTemp.sinkClassID);
2195         sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &subQuery, NULL);
2196
2197         while ((eCode = sqlite3_step(subQuery)) == SQLITE_ROW)
2198         {
2199             propertyTemp.classProperty = (am_ClassProperty_e) sqlite3_column_int(subQuery, 0);
2200             propertyTemp.value = sqlite3_column_int(subQuery, 1);
2201             classTemp.listClassProperties.push_back(propertyTemp);
2202         }
2203
2204         if (eCode != SQLITE_DONE)
2205         {
2206             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE error code:"), DLT_INT(eCode));
2207             return E_DATABASE_ERROR;
2208         }
2209
2210         if ((eCode = sqlite3_finalize(subQuery)) != SQLITE_OK)
2211         {
2212             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:"), DLT_INT(eCode));
2213             return E_DATABASE_ERROR;
2214         }
2215         listSinkClasses.push_back(classTemp);
2216     }
2217
2218     if (eCode != SQLITE_DONE)
2219     {
2220         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE error code:"), DLT_INT(eCode));
2221         return E_DATABASE_ERROR;
2222     }
2223
2224     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2225     {
2226         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSourceClasses SQLITE Finalize error code:"), DLT_INT(eCode));
2227         return E_DATABASE_ERROR;
2228     }
2229
2230     return E_OK;
2231 }
2232
2233 am_Error_e DatabaseHandler::getListVisibleMainConnections(std::vector<am_MainConnectionType_s> & listConnections) const
2234 {
2235     listConnections.clear();
2236     sqlite3_stmt *query = NULL;
2237     int eCode = 0;
2238     am_MainConnectionType_s temp;
2239
2240     std::string command = "SELECT mainConnectionID, sourceID, sinkID, connectionState, delay FROM " + std::string(MAINCONNECTION_TABLE);
2241     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2242
2243     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2244     {
2245         temp.mainConnectionID = sqlite3_column_int(query, 0);
2246         temp.sourceID = sqlite3_column_int(query, 1);
2247         temp.sinkID = sqlite3_column_int(query, 2);
2248         temp.connectionState = (am_ConnectionState_e) sqlite3_column_int(query, 3);
2249         temp.delay = sqlite3_column_int(query, 4);
2250         listConnections.push_back(temp);
2251     }
2252
2253     if (eCode != SQLITE_DONE)
2254     {
2255         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListVisibleMainConnections SQLITE error code:"), DLT_INT(eCode));
2256         return E_DATABASE_ERROR;
2257     }
2258
2259     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2260     {
2261         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListVisibleMainConnections SQLITE Finalize error code:"), DLT_INT(eCode));
2262         return E_DATABASE_ERROR;
2263     }
2264
2265     return E_OK;
2266 }
2267
2268 am_Error_e DatabaseHandler::getListMainSinks(std::vector<am_SinkType_s> & listMainSinks) const
2269 {
2270     listMainSinks.clear();
2271     sqlite3_stmt* query = NULL;
2272     int eCode = 0;
2273     am_SinkType_s temp;
2274
2275     std::string command = "SELECT name, sinkID, availability, availabilityReason, muteState, mainVolume, sinkClassID FROM " + std::string(SINK_TABLE) + " WHERE visible=1 AND reserved=0";
2276     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2277
2278     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2279     {
2280         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2281         temp.sinkID = sqlite3_column_int(query, 1);
2282         temp.availability.availability = (am_Availablility_e) sqlite3_column_int(query, 2);
2283         temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2284         temp.muteState = (am_MuteState_e) sqlite3_column_int(query, 4);
2285         temp.volume = sqlite3_column_int(query, 5);
2286         temp.sinkClassID = sqlite3_column_int(query, 6);
2287         listMainSinks.push_back(temp);
2288     }
2289
2290     if (eCode != SQLITE_DONE)
2291     {
2292         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE error code:"), DLT_INT(eCode));
2293         return E_DATABASE_ERROR;
2294     }
2295
2296     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2297     {
2298         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinks SQLITE Finalize error code:"), DLT_INT(eCode));
2299         return E_DATABASE_ERROR;
2300     }
2301
2302     return E_OK;
2303 }
2304
2305 am_Error_e DatabaseHandler::getListMainSources(std::vector<am_SourceType_s> & listMainSources) const
2306 {
2307     listMainSources.clear();
2308     sqlite3_stmt* query = NULL;
2309     int eCode = 0;
2310     am_SourceType_s temp;
2311     std::string command = "SELECT name, sourceClassID, availability, availabilityReason, sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE visible=1";
2312     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2313
2314     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2315     {
2316         temp.name = std::string((const char*) sqlite3_column_text(query, 0));
2317         temp.sourceClassID = sqlite3_column_int(query, 1);
2318         temp.availability.availability = (am_Availablility_e) sqlite3_column_int(query, 2);
2319         temp.availability.availabilityReason = (am_AvailabilityReason_e) sqlite3_column_int(query, 3);
2320         temp.sourceID = sqlite3_column_int(query, 4);
2321
2322         listMainSources.push_back(temp);
2323     }
2324
2325     if (eCode != SQLITE_DONE)
2326     {
2327         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE error code:"), DLT_INT(eCode));
2328         return E_DATABASE_ERROR;
2329     }
2330
2331     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2332     {
2333         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
2334         return E_DATABASE_ERROR;
2335     }
2336
2337     return E_OK;
2338 }
2339
2340 am_Error_e DatabaseHandler::getListMainSinkSoundProperties(const am_sinkID_t sinkID, std::vector<am_MainSoundProperty_s> & listSoundProperties) const
2341 {
2342     assert(sinkID!=0);
2343     if (!existSink(sinkID))
2344         return E_DATABASE_ERROR; // todo: here we could change to non existen, but not shown in sequences
2345     listSoundProperties.clear();
2346
2347     sqlite3_stmt* query = NULL;
2348     int eCode = 0;
2349     am_MainSoundProperty_s temp;
2350     std::string command = "SELECT soundPropertyType, value FROM SinkMainSoundProperty" + i2s(sinkID);
2351     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2352
2353     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2354     {
2355         temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2356         temp.value = sqlite3_column_int(query, 1);
2357         listSoundProperties.push_back(temp);
2358     }
2359
2360     if (eCode != SQLITE_DONE)
2361     {
2362         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:"), DLT_INT(eCode));
2363         return E_DATABASE_ERROR;
2364     }
2365
2366     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2367     {
2368         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainSinkSoundProperties SQLITE Finalize error code:"), DLT_INT(eCode));
2369         return E_DATABASE_ERROR;
2370     }
2371
2372     return E_OK;
2373 }
2374
2375 am_Error_e DatabaseHandler::getListMainSourceSoundProperties(const am_sourceID_t sourceID, std::vector<am_MainSoundProperty_s> & listSourceProperties) const
2376 {
2377     assert(sourceID!=0);
2378     if (!existSource(sourceID))
2379         return E_DATABASE_ERROR; // todo: here we could change to non existen, but not shown in sequences
2380     listSourceProperties.clear();
2381
2382     sqlite3_stmt* query = NULL;
2383     int eCode = 0;
2384     am_MainSoundProperty_s temp;
2385     std::string command = "SELECT soundPropertyType, value FROM SourceMainSoundProperty" + i2s(sourceID);
2386     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2387
2388     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2389     {
2390         temp.type = (am_MainSoundPropertyType_e) sqlite3_column_int(query, 0);
2391         temp.value = sqlite3_column_int(query, 1);
2392         listSourceProperties.push_back(temp);
2393     }
2394
2395     if (eCode != SQLITE_DONE)
2396     {
2397         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainSinkSoundProperties SQLITE error code:"), DLT_INT(eCode));
2398         return E_DATABASE_ERROR;
2399     }
2400
2401     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2402     {
2403         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListMainSinkSoundProperties SQLITE Finalize error code:"), DLT_INT(eCode));
2404         return E_DATABASE_ERROR;
2405     }
2406
2407     return E_OK;
2408 }
2409
2410 am_Error_e DatabaseHandler::getListSystemProperties(std::vector<am_SystemProperty_s> & listSystemProperties) const
2411 {
2412     listSystemProperties.clear();
2413
2414     sqlite3_stmt* query = NULL;
2415     int eCode = 0;
2416     am_SystemProperty_s temp;
2417     std::string command = "SELECT type, value FROM " + std::string(SYSTEM_TABLE);
2418     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2419
2420     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2421     {
2422         temp.type = (am_SystemPropertyType_e) sqlite3_column_int(query, 0);
2423         temp.value = sqlite3_column_int(query, 1);
2424         listSystemProperties.push_back(temp);
2425     }
2426
2427     if (eCode != SQLITE_DONE)
2428     {
2429         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSystemProperties SQLITE error code:"), DLT_INT(eCode));
2430         return E_DATABASE_ERROR;
2431     }
2432
2433     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2434     {
2435         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSystemProperties SQLITE Finalize error code:"), DLT_INT(eCode));
2436         return E_DATABASE_ERROR;
2437     }
2438
2439     return E_OK;
2440 }
2441
2442 am_Error_e am::DatabaseHandler::getListSinkConnectionFormats(const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2443 {
2444     listConnectionFormats.clear();
2445     sqlite3_stmt *qConnectionFormat = NULL;
2446     int eCode = 0;
2447     am_ConnectionFormat_e tempConnectionFormat;
2448     std::string commandConnectionFormat = "SELECT soundFormat FROM SinkConnectionFormat" + i2s(sinkID);
2449     sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
2450     while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2451     {
2452         tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2453         listConnectionFormats.push_back(tempConnectionFormat);
2454     }
2455
2456     if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
2457     {
2458         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSinkConnectionFormats SQLITE Finalize error code:"), DLT_INT(eCode));
2459         return E_DATABASE_ERROR;
2460     }
2461
2462     return E_OK;
2463 }
2464
2465 am_Error_e am::DatabaseHandler::getListSourceConnectionFormats(const am_sourceID_t sourceID, std::vector<am_ConnectionFormat_e> & listConnectionFormats) const
2466 {
2467     listConnectionFormats.clear();
2468     sqlite3_stmt* qConnectionFormat = NULL;
2469     int eCode = 0;
2470     am_ConnectionFormat_e tempConnectionFormat;
2471
2472     //read out the connectionFormats
2473     std::string commandConnectionFormat = "SELECT soundFormat FROM SourceConnectionFormat" + i2s(sourceID);
2474     sqlite3_prepare_v2(mDatabase, commandConnectionFormat.c_str(), -1, &qConnectionFormat, NULL);
2475     while ((eCode = sqlite3_step(qConnectionFormat)) == SQLITE_ROW)
2476     {
2477         tempConnectionFormat = (am_ConnectionFormat_e) sqlite3_column_int(qConnectionFormat, 0);
2478         listConnectionFormats.push_back(tempConnectionFormat);
2479     }
2480
2481     if ((eCode = sqlite3_finalize(qConnectionFormat)) != SQLITE_OK)
2482     {
2483         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListSources SQLITE Finalize error code:"), DLT_INT(eCode));
2484         return E_DATABASE_ERROR;
2485     }
2486
2487     return E_OK;
2488 }
2489
2490 am_Error_e am::DatabaseHandler::getListGatewayConnectionFormats(const am_gatewayID_t gatewayID, std::vector<bool> & listConnectionFormat) const
2491 {
2492     ListConnectionFormat::const_iterator iter = mListConnectionFormat.begin();
2493     iter = mListConnectionFormat.find(gatewayID);
2494     if (iter == mListConnectionFormat.end())
2495     {
2496         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getListGatewayConnectionFormats database error with convertionFormat"));
2497         return (E_DATABASE_ERROR);
2498     }
2499     listConnectionFormat = iter->second;
2500
2501     return (E_OK);
2502 }
2503
2504 am_Error_e DatabaseHandler::getTimingInformation(const am_mainConnectionID_t mainConnectionID, am_timeSync_t & delay) const
2505 {
2506     assert(mainConnectionID!=0);
2507     delay = -1;
2508     sqlite3_stmt *query = NULL;
2509     int eCode = 0;
2510
2511     std::string command = "SELECT delay FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
2512     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2513
2514     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
2515     {
2516         delay = sqlite3_column_int(query, 0);
2517     }
2518
2519     if (eCode != SQLITE_DONE)
2520     {
2521         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getTimingInformation SQLITE error code:"), DLT_INT(eCode));
2522         return E_DATABASE_ERROR;
2523     }
2524
2525     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2526     {
2527         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getTimingInformation SQLITE Finalize error code:"), DLT_INT(eCode));
2528         return E_DATABASE_ERROR;
2529     }
2530
2531     if (delay == -1)
2532         return E_NOT_POSSIBLE;
2533
2534     return E_OK;
2535 }
2536
2537 bool DatabaseHandler::sqQuery(const std::string& query)
2538 {
2539     sqlite3_stmt* statement;
2540     int eCode = 0;
2541     if ((eCode = sqlite3_exec(mDatabase, query.c_str(), NULL, &statement, NULL)) != SQLITE_OK)
2542     {
2543         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::sqQuery SQL Query failed:"), DLT_STRING(query.c_str()), DLT_STRING("error code:"), DLT_INT(eCode));
2544         return false;
2545     }
2546     return true;
2547 }
2548
2549 bool DatabaseHandler::openDatabase()
2550 {
2551     if (sqlite3_open_v2(mPath.c_str(), &mDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK)
2552     {
2553         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::openDatabase opened database"));
2554         return true;
2555     }DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::openDatabase failed to open database"));
2556     return false;
2557 }
2558
2559 am_Error_e DatabaseHandler::changeDelayMainConnection(const am_timeSync_t & delay, const am_mainConnectionID_t & connectionID)
2560 {
2561     assert(connectionID!=0);
2562
2563     sqlite3_stmt* query = NULL;
2564     int eCode = 0;
2565     std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE delay=? AND mainConnectionID=?";
2566     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2567     sqlite3_bind_int(query, 1, delay);
2568     sqlite3_bind_int(query, 2, connectionID);
2569     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2570     {
2571         sqlite3_finalize(query);
2572         return E_OK;
2573     }
2574     command = "UPDATE " + std::string(MAINCONNECTION_TABLE) + " SET delay=? WHERE mainConnectionID=?;";
2575     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2576     sqlite3_bind_int(query, 1, delay);
2577     sqlite3_bind_int(query, 2, connectionID);
2578
2579     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2580     {
2581         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeDelayMainConnection SQLITE Step error code:"), DLT_INT(eCode));
2582         return E_DATABASE_ERROR;
2583     }
2584
2585     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2586     {
2587         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeDelayMainConnection SQLITE Finalize error code:"), DLT_INT(eCode));
2588         return E_DATABASE_ERROR;
2589     }
2590
2591     if (mDatabaseObserver)
2592         mDatabaseObserver->timingInformationChanged(connectionID, delay);
2593
2594     return E_OK;
2595 }
2596
2597 am_Error_e DatabaseHandler::enterConnectionDB(const am_Connection_s& connection, am_connectionID_t& connectionID)
2598 {
2599     assert(connection.connectionID==0);
2600     assert(connection.sinkID!=0);
2601     assert(connection.sourceID!=0);
2602     //connection format is not checked, because it's project specific
2603
2604     sqlite3_stmt* query = NULL;
2605     int eCode = 0;
2606     std::string command = "INSERT INTO " + std::string(CONNECTION_TABLE) + "(sinkID, sourceID, delay, connectionFormat, reserved) VALUES (?,?,?,?,?)";
2607
2608     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2609     sqlite3_bind_int(query, 1, connection.sinkID);
2610     sqlite3_bind_int(query, 2, connection.sourceID);
2611     sqlite3_bind_int(query, 3, connection.delay);
2612     sqlite3_bind_int(query, 4, connection.connectionFormat);
2613     sqlite3_bind_int(query, 5, true);
2614
2615     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2616     {
2617         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterConnectionDB SQLITE Step error code:"), DLT_INT(eCode));
2618         return E_DATABASE_ERROR;
2619     }
2620
2621     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2622     {
2623         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterConnectionDB SQLITE Finalize error code:"), DLT_INT(eCode));
2624         return E_DATABASE_ERROR;
2625     }
2626
2627     connectionID = sqlite3_last_insert_rowid(mDatabase);
2628
2629     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterConnectionDB entered new connection sourceID:"), DLT_INT16(connection.sourceID), DLT_STRING("sinkID:"), DLT_INT16(connection.sinkID), DLT_STRING("sourceID:"), DLT_INT16(connection.sourceID), DLT_STRING("delay:"), DLT_INT16(connection.delay), DLT_STRING("connectionFormat:"), DLT_INT16(connection.connectionFormat), DLT_STRING("assigned ID:"), DLT_INT16(connectionID));
2630     return E_OK;
2631 }
2632
2633 am_Error_e DatabaseHandler::enterSinkClassDB(const am_SinkClass_s & sinkClass, am_sinkClass_t & sinkClassID)
2634 {
2635     assert(sinkClass.sinkClassID<DYNAMIC_ID_BOUNDARY);
2636     assert(!sinkClass.listClassProperties.empty());
2637     assert(!sinkClass.name.empty());
2638
2639     sqlite3_stmt* query = NULL;
2640     int eCode = 0;
2641     std::string command;
2642
2643     //if sinkID is zero and the first Static Sink was already entered, the ID is created
2644     if (sinkClass.sinkClassID == 0 && !mFirstStaticSinkClass)
2645     {
2646         command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name) VALUES (?)";
2647     }
2648     else
2649     {
2650         //check if the ID already exists
2651         if (existSinkClass(sinkClass.sinkClassID))
2652             return E_ALREADY_EXISTS;
2653         command = "INSERT INTO " + std::string(SINK_CLASS_TABLE) + "(name, sinkClassID) VALUES (?,?)";
2654     }
2655
2656     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2657     sqlite3_bind_text(query, 1, sinkClass.name.c_str(), sinkClass.name.size(), SQLITE_STATIC);
2658
2659     //if the ID is not created, we add it to the query
2660     if (sinkClass.sinkClassID != 0)
2661     {
2662         sqlite3_bind_int(query, 2, sinkClass.sinkClassID);
2663     }
2664
2665     //if the first static sink is entered, we need to set it onto the boundary
2666     else if (mFirstStaticSinkClass)
2667     {
2668         sqlite3_bind_int(query, 2, DYNAMIC_ID_BOUNDARY);
2669         mFirstStaticSinkClass = false;
2670     }
2671
2672     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2673     {
2674         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkClassDB SQLITE Step error code:"), DLT_INT(eCode));
2675         return E_DATABASE_ERROR;
2676     }
2677
2678     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2679     {
2680         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkClassDB SQLITE Finalize error code:"), DLT_INT(eCode));
2681         return E_DATABASE_ERROR;
2682     }
2683
2684     sinkClassID = sqlite3_last_insert_rowid(mDatabase); //todo:change last_insert implementations for mulithread usage...
2685
2686     //now we need to create the additional tables:
2687     command = "CREATE TABLE SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty INTEGER, value INTEGER)");
2688     assert(this->sqQuery(command));
2689
2690     //fill ConnectionFormats
2691     command = "INSERT INTO SinkClassProperties" + i2s(sinkClassID) + std::string("(classProperty,value) VALUES (?,?)");
2692     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2693     std::vector<am_ClassProperty_s>::const_iterator Iterator = sinkClass.listClassProperties.begin();
2694     for (; Iterator < sinkClass.listClassProperties.end(); ++Iterator)
2695     {
2696         sqlite3_bind_int(query, 1, Iterator->classProperty);
2697         sqlite3_bind_int(query, 2, Iterator->value);
2698         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2699         {
2700             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkClassDB SQLITE Step error code:"), DLT_INT(eCode));
2701             return E_DATABASE_ERROR;
2702         }
2703         sqlite3_reset(query);
2704     }
2705
2706     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2707     {
2708         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSinkClassDB SQLITE Finalize error code:"), DLT_INT(eCode));
2709         return E_DATABASE_ERROR;
2710     }
2711
2712     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterSinkClassDB entered new sinkClass"));
2713     if (mDatabaseObserver)
2714         mDatabaseObserver->numberOfSinkClassesChanged();
2715     return E_OK;
2716 }
2717
2718 am_Error_e DatabaseHandler::enterSourceClassDB(am_sourceClass_t & sourceClassID, const am_SourceClass_s & sourceClass)
2719 {
2720     assert(sourceClass.sourceClassID<DYNAMIC_ID_BOUNDARY);
2721     assert(!sourceClass.listClassProperties.empty());
2722     assert(!sourceClass.name.empty());
2723
2724     sqlite3_stmt* query = NULL;
2725     int eCode = 0;
2726     std::string command;
2727
2728     //if sinkID is zero and the first Static Sink was already entered, the ID is created
2729     if (sourceClass.sourceClassID == 0 && !mFirstStaticSourceClass)
2730     {
2731         command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name) VALUES (?)";
2732     }
2733     else
2734     {
2735         //check if the ID already exists
2736         if (existSourceClass(sourceClass.sourceClassID))
2737             return E_ALREADY_EXISTS;
2738         command = "INSERT INTO " + std::string(SOURCE_CLASS_TABLE) + "(name, sourceClassID) VALUES (?,?)";
2739     }
2740
2741     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2742     sqlite3_bind_text(query, 1, sourceClass.name.c_str(), sourceClass.name.size(), SQLITE_STATIC);
2743
2744     //if the ID is not created, we add it to the query
2745     if (sourceClass.sourceClassID != 0)
2746     {
2747         sqlite3_bind_int(query, 2, sourceClass.sourceClassID);
2748     }
2749
2750     //if the first static sink is entered, we need to set it onto the boundary
2751     else if (mFirstStaticSourceClass)
2752     {
2753         sqlite3_bind_int(query, 2, DYNAMIC_ID_BOUNDARY);
2754         mFirstStaticSourceClass = false;
2755     }
2756
2757     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2758     {
2759         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceClassDB SQLITE Step error code:"), DLT_INT(eCode));
2760         return E_DATABASE_ERROR;
2761     }
2762
2763     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2764     {
2765         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceClassDB SQLITE Finalize error code:"), DLT_INT(eCode));
2766         return E_DATABASE_ERROR;
2767     }
2768
2769     sourceClassID = sqlite3_last_insert_rowid(mDatabase); //todo:change last_insert implementations for mulithread usage...
2770
2771     //now we need to create the additional tables:
2772     command = "CREATE TABLE SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty INTEGER, value INTEGER)");
2773     assert(sqQuery(command));
2774
2775     //fill ConnectionFormats
2776     command = "INSERT INTO SourceClassProperties" + i2s(sourceClassID) + std::string("(classProperty,value) VALUES (?,?)");
2777     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2778     std::vector<am_ClassProperty_s>::const_iterator Iterator = sourceClass.listClassProperties.begin();
2779     for (; Iterator < sourceClass.listClassProperties.end(); ++Iterator)
2780     {
2781         sqlite3_bind_int(query, 1, Iterator->classProperty);
2782         sqlite3_bind_int(query, 2, Iterator->value);
2783         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2784         {
2785             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceClassDB SQLITE Step error code:"), DLT_INT(eCode));
2786             return E_DATABASE_ERROR;
2787         }
2788         sqlite3_reset(query);
2789     }
2790
2791     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2792     {
2793         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSourceClassDB SQLITE Finalize error code:"), DLT_INT(eCode));
2794         return E_DATABASE_ERROR;
2795     }
2796
2797     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterSourceClassDB entered new sourceClass"));
2798
2799     if (mDatabaseObserver)
2800         mDatabaseObserver->numberOfSourceClassesChanged();
2801     return E_OK;
2802 }
2803
2804 am_Error_e DatabaseHandler::enterSystemProperties(const std::vector<am_SystemProperty_s> & listSystemProperties)
2805 {
2806     sqlite3_stmt* query = NULL;
2807     int eCode = 0;
2808     std::vector<am_SystemProperty_s>::const_iterator listIterator = listSystemProperties.begin();
2809     std::string command = "DELETE * FROM " + std::string(SYSTEM_TABLE);
2810     sqQuery(command);
2811
2812     command = "INSERT INTO " + std::string(SYSTEM_TABLE) + " (type, value) VALUES (?,?)";
2813
2814     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2815     for (; listIterator < listSystemProperties.end(); ++listIterator)
2816     {
2817         sqlite3_bind_int(query, 1, listIterator->type);
2818         sqlite3_bind_int(query, 2, listIterator->value);
2819
2820         if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
2821         {
2822             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSystemProperties SQLITE Step error code:"), DLT_INT(eCode));
2823             return E_DATABASE_ERROR;
2824         }
2825
2826         sqlite3_reset(query);
2827     }
2828
2829     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
2830     {
2831         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::enterSystemProperties SQLITE Finalize error code:"), DLT_INT(eCode));
2832         return E_DATABASE_ERROR;
2833     }
2834
2835     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::enterSystemProperties entered system properties"));
2836     return E_OK;
2837 }
2838
2839 bool DatabaseHandler::existMainConnection(const am_mainConnectionID_t mainConnectionID) const
2840 {
2841     sqlite3_stmt* query = NULL;
2842     std::string command = "SELECT mainConnectionID FROM " + std::string(MAINCONNECTION_TABLE) + " WHERE mainConnectionID=" + i2s(mainConnectionID);
2843     int eCode = 0;
2844     bool returnVal = true;
2845     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2846     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2847         returnVal = false;
2848     else if (eCode != SQLITE_ROW)
2849     {
2850         returnVal = false;
2851         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existMainConnection database error!:"), DLT_INT(eCode))
2852     }
2853     sqlite3_finalize(query);
2854     return returnVal;
2855 }
2856
2857 bool DatabaseHandler::existSource(const am_sourceID_t sourceID) const
2858 {
2859     sqlite3_stmt* query = NULL;
2860     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND sourceID=" + i2s(sourceID);
2861     int eCode = 0;
2862     bool returnVal = true;
2863     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2864     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2865         returnVal = false;
2866     else if (eCode != SQLITE_ROW)
2867     {
2868         returnVal = false;
2869         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSource database error!:"), DLT_INT(eCode))
2870     }
2871     sqlite3_finalize(query);
2872     return returnVal;
2873 }
2874
2875 bool DatabaseHandler::existSourceNameOrID(const am_sourceID_t sourceID, const std::string & name) const
2876 {
2877     sqlite3_stmt* query = NULL;
2878     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND (name=? OR sourceID=?)";
2879     int eCode = 0;
2880     bool returnVal = true;
2881     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2882     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2883     sqlite3_bind_int(query, 2, sourceID);
2884     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2885         returnVal = false;
2886     else if (eCode != SQLITE_ROW)
2887     {
2888         returnVal = false;
2889         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSource database error!:"), DLT_INT(eCode))
2890     }
2891     sqlite3_finalize(query);
2892     return returnVal;
2893 }
2894
2895 bool DatabaseHandler::existSourceName(const std::string & name) const
2896 {
2897     sqlite3_stmt* query = NULL;
2898     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE reserved=0 AND name=?";
2899     int eCode = 0;
2900     bool returnVal = true;
2901     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2902     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2903     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2904         returnVal = false;
2905     else if (eCode != SQLITE_ROW)
2906     {
2907         returnVal = false;
2908         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSource database error!:"), DLT_INT(eCode))
2909     }
2910     sqlite3_finalize(query);
2911     return returnVal;
2912 }
2913
2914 bool DatabaseHandler::existSink(const am_sinkID_t sinkID) const
2915 {
2916     sqlite3_stmt* query = NULL;
2917     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
2918     int eCode = 0;
2919     bool returnVal = true;
2920     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2921     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2922         returnVal = false;
2923     else if (eCode != SQLITE_ROW)
2924     {
2925         returnVal = false;
2926         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSink database error!:"), DLT_INT(eCode))
2927     }
2928     sqlite3_finalize(query);
2929     return returnVal;
2930 }
2931
2932 bool DatabaseHandler::existSinkNameOrID(const am_sinkID_t sinkID, const std::string & name) const
2933 {
2934     sqlite3_stmt* query = NULL;
2935     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND (name=? OR sinkID=?)";
2936     int eCode = 0;
2937     bool returnVal = true;
2938     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2939     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2940     sqlite3_bind_int(query, 2, sinkID);
2941     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2942         returnVal = false;
2943     else if (eCode != SQLITE_ROW)
2944     {
2945         returnVal = false;
2946         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSink database error!:"), DLT_INT(eCode))
2947     }
2948     sqlite3_finalize(query);
2949     return returnVal;
2950 }
2951
2952 bool DatabaseHandler::existSinkName(const std::string & name) const
2953 {
2954     sqlite3_stmt* query = NULL;
2955     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND name=?";
2956     int eCode = 0;
2957     bool returnVal = true;
2958     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2959     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
2960     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2961         returnVal = false;
2962     else if (eCode != SQLITE_ROW)
2963     {
2964         returnVal = false;
2965         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSink database error!:"), DLT_INT(eCode))
2966     }
2967     sqlite3_finalize(query);
2968     return returnVal;
2969 }
2970
2971 bool DatabaseHandler::existDomain(const am_domainID_t domainID) const
2972 {
2973     sqlite3_stmt* query = NULL;
2974     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE reserved=0 AND domainID=" + i2s(domainID);
2975     int eCode = 0;
2976     bool returnVal = true;
2977     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2978     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2979         returnVal = false;
2980     else if (eCode != SQLITE_ROW)
2981     {
2982         returnVal = false;
2983         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existDomain database error!:"), DLT_INT(eCode))
2984     }
2985     sqlite3_finalize(query);
2986     return returnVal;
2987 }
2988
2989 bool DatabaseHandler::existGateway(const am_gatewayID_t gatewayID) const
2990 {
2991     sqlite3_stmt* query = NULL;
2992     std::string command = "SELECT gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE gatewayID=" + i2s(gatewayID);
2993     int eCode = 0;
2994     bool returnVal = true;
2995     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
2996     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
2997         returnVal = false;
2998     else if (eCode != SQLITE_ROW)
2999     {
3000         returnVal = false;
3001         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existGateway database error!:"), DLT_INT(eCode))
3002     }
3003     sqlite3_finalize(query);
3004     return returnVal;
3005 }
3006
3007 am_Error_e DatabaseHandler::getDomainOfSource(const am_sourceID_t sourceID, am_domainID_t & domainID) const
3008 {
3009     assert(sourceID!=0);
3010
3011     sqlite3_stmt* query = NULL;
3012     std::string command = "SELECT domainID FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3013     int eCode = 0;
3014     am_Error_e returnVal = E_DATABASE_ERROR;
3015     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3016     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3017     {
3018         domainID = sqlite3_column_int(query, 0);
3019         returnVal = E_OK;
3020     }
3021     else
3022     {
3023         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getDomainOfSource database error!:"), DLT_INT(eCode))
3024     }
3025     sqlite3_finalize(query);
3026     return (returnVal);
3027 }
3028
3029 am_Error_e am::DatabaseHandler::getDomainOfSink(const am_sinkID_t sinkID, am_domainID_t & domainID) const
3030 {
3031     assert(sinkID!=0);
3032
3033     sqlite3_stmt* query = NULL;
3034     std::string command = "SELECT domainID FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3035     int eCode = 0;
3036     am_Error_e returnVal = E_DATABASE_ERROR;
3037     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3038     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3039     {
3040         domainID = sqlite3_column_int(query, 0);
3041         returnVal = E_OK;
3042     }
3043     else
3044     {
3045         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getDomainOfSink database error!:"), DLT_INT(eCode))
3046     }
3047     sqlite3_finalize(query);
3048     return (returnVal);
3049 }
3050
3051 bool DatabaseHandler::existSinkClass(const am_sinkClass_t sinkClassID) const
3052 {
3053     sqlite3_stmt* query = NULL;
3054     std::string command = "SELECT sinkClassID FROM " + std::string(SINK_CLASS_TABLE) + " WHERE sinkClassID=" + i2s(sinkClassID);
3055     int eCode = 0;
3056     bool returnVal = true;
3057     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3058     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3059         returnVal = false;
3060     else if (eCode != SQLITE_ROW)
3061     {
3062         returnVal = false;
3063         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSinkClass database error!:"), DLT_INT(eCode))
3064     }
3065     sqlite3_finalize(query);
3066     return returnVal;
3067 }
3068
3069 bool DatabaseHandler::existSourceClass(const am_sourceClass_t sourceClassID) const
3070 {
3071     sqlite3_stmt* query = NULL;
3072     std::string command = "SELECT sourceClassID FROM " + std::string(SOURCE_CLASS_TABLE) + " WHERE sourceClassID=" + i2s(sourceClassID);
3073     int eCode = 0;
3074     bool returnVal = true;
3075     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3076     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3077         returnVal = false;
3078     else if (eCode != SQLITE_ROW)
3079     {
3080         returnVal = false;
3081         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existSinkClass database error!:"), DLT_INT(eCode))
3082     }
3083     sqlite3_finalize(query);
3084     return returnVal;
3085 }
3086
3087 am_Error_e DatabaseHandler::changeConnectionTimingInformation(const am_connectionID_t connectionID, const am_timeSync_t delay)
3088 {
3089     assert(connectionID!=0);
3090
3091     sqlite3_stmt *query = NULL, *queryMainConnections, *queryMainConnectionSubIDs;
3092     int eCode = 0, eCode1 = 0;
3093     std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set delay=? WHERE connectionID=?";
3094
3095     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3096     sqlite3_bind_int(query, 1, delay);
3097     sqlite3_bind_int(query, 2, connectionID);
3098
3099     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3100     {
3101         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionTimingInformation SQLITE Step error code:"), DLT_INT(eCode));
3102         return E_DATABASE_ERROR;
3103     }
3104
3105     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3106     {
3107         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionTimingInformation SQLITE Finalize error code:"), DLT_INT(eCode));
3108         return E_DATABASE_ERROR;
3109     }
3110
3111     //now we need to find all mainConnections that use the changed connection and update their timing
3112
3113     int tempMainConnectionID;
3114     //first get all route tables for all mainconnections
3115     command = "SELECT name FROM sqlite_master WHERE type ='table' and name LIKE 'MainConnectionRoute%'";
3116     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryMainConnections, NULL);
3117
3118     while ((eCode = sqlite3_step(queryMainConnections)) == SQLITE_ROW)
3119     {
3120         //now check if the connection ID is in this table
3121         std::string tablename = std::string((const char*) sqlite3_column_text(queryMainConnections, 0));
3122         std::string command2 = "SELECT connectionID FROM " + tablename + " WHERE connectionID=" + i2s(connectionID);
3123         sqlite3_prepare_v2(mDatabase, command2.c_str(), -1, &queryMainConnectionSubIDs, NULL);
3124         if ((eCode1 = sqlite3_step(queryMainConnectionSubIDs)) == SQLITE_ROW)
3125         {
3126             //if the connection ID is in, recalculate the mainconnection delay
3127             std::stringstream(tablename.substr(tablename.find_first_not_of("MainConnectionRoute"))) >> tempMainConnectionID;
3128             changeDelayMainConnection(calculateMainConnectionDelay(tempMainConnectionID), tempMainConnectionID);
3129         }
3130         else if (eCode1 != SQLITE_DONE)
3131         {
3132             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:"), DLT_INT(eCode1));
3133             return E_DATABASE_ERROR;
3134         }
3135     }
3136
3137     if (eCode != SQLITE_DONE)
3138     {
3139         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionTimingInformation SQLITE error code:"), DLT_INT(eCode));
3140         return E_DATABASE_ERROR;
3141     }
3142
3143     if ((eCode = sqlite3_finalize(queryMainConnections)) != SQLITE_OK)
3144     {
3145         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionTimingInformation SQLITE Finalize error code:"), DLT_INT(eCode));
3146         return E_DATABASE_ERROR;
3147     }
3148
3149     return E_OK;
3150 }
3151
3152 am_Error_e DatabaseHandler::changeConnectionFinal(const am_connectionID_t connectionID)
3153 {
3154     assert(connectionID!=0);
3155
3156     sqlite3_stmt *query = NULL;
3157     int eCode = 0;
3158     std::string command = "UPDATE " + std::string(CONNECTION_TABLE) + " set reserved=0 WHERE connectionID=?";
3159
3160     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3161     sqlite3_bind_int(query, 1, connectionID);
3162
3163     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3164     {
3165         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionFinal SQLITE Step error code:"), DLT_INT(eCode));
3166         return E_DATABASE_ERROR;
3167     }
3168
3169     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3170     {
3171         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeConnectionFinal SQLITE Finalize error code:"), DLT_INT(eCode));
3172         return E_DATABASE_ERROR;
3173     }
3174     return E_OK;
3175 }
3176
3177 am_timeSync_t DatabaseHandler::calculateMainConnectionDelay(const am_mainConnectionID_t mainConnectionID) const
3178 {
3179     assert(mainConnectionID!=0);
3180     sqlite3_stmt* query = NULL;
3181     std::string command = "SELECT sum(Connections.delay),min(Connections.delay) FROM " + std::string(CONNECTION_TABLE) + ",MainConnectionRoute" + i2s(mainConnectionID) + " WHERE MainConnectionRoute" + i2s(mainConnectionID) + ".connectionID = Connections.connectionID";
3182     int eCode = 0;
3183     am_timeSync_t delay = 0;
3184     am_timeSync_t min = 0;
3185     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3186     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3187     {
3188         delay = sqlite3_column_int(query, 0);
3189         min = sqlite3_column_int(query, 1);
3190     }
3191     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3192     {
3193         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::calculateMainConnectionDelay SQLITE Step error code:"), DLT_INT(eCode));
3194         return E_DATABASE_ERROR;
3195     }
3196
3197     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3198     {
3199         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::calculateMainConnectionDelay SQLITE Finalize error code:"), DLT_INT(eCode));
3200         return E_DATABASE_ERROR;
3201     }
3202     if (min < 0)
3203         delay = -1;
3204     return delay;
3205
3206 }
3207
3208 void DatabaseHandler::registerObserver(DatabaseObserver *iObserver)
3209 {
3210     assert(iObserver!=NULL);
3211     mDatabaseObserver = iObserver;
3212 }
3213
3214 bool DatabaseHandler::sourceVisible(const am_sourceID_t sourceID) const
3215 {
3216     assert(sourceID!=0);
3217     sqlite3_stmt* query = NULL;
3218     std::string command = "SELECT visible FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3219     int eCode = 0;
3220     bool returnVal = false;
3221     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3222     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3223     {
3224         returnVal = (bool) sqlite3_column_int(query, 0);
3225     }
3226     else if (eCode != SQLITE_ROW)
3227     {
3228         returnVal = false;
3229         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::sourceVisible database error!:"), DLT_INT(eCode))
3230     }
3231     sqlite3_finalize(query);
3232     return returnVal;
3233 }
3234
3235 bool DatabaseHandler::sinkVisible(const am_sinkID_t sinkID) const
3236 {
3237     sqlite3_stmt* query = NULL;
3238     std::string command = "SELECT visible FROM " + std::string(SINK_TABLE) + " WHERE reserved=0 AND sinkID=" + i2s(sinkID);
3239     int eCode = 0;
3240     bool returnVal = false;
3241     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3242     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3243     {
3244         returnVal = sqlite3_column_int(query, 0);
3245     }
3246     else if (eCode != SQLITE_ROW)
3247     {
3248         returnVal = false;
3249         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::sinkVisible database error!:"), DLT_INT(eCode))
3250     }
3251     sqlite3_finalize(query);
3252     return returnVal;
3253 }
3254
3255 bool DatabaseHandler::existConnection(const am_Connection_s connection)
3256 {
3257     sqlite3_stmt* query = NULL;
3258     std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE sinkID=? AND sourceID=? AND connectionFormat=? AND reserved=0";
3259     int eCode = 0;
3260     bool returnVal = true;
3261     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3262     sqlite3_bind_int(query, 1, connection.sinkID);
3263     sqlite3_bind_int(query, 2, connection.sourceID);
3264     sqlite3_bind_int(query, 3, connection.connectionFormat);
3265     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3266         returnVal = false;
3267     else if (eCode != SQLITE_ROW)
3268     {
3269         returnVal = false;
3270         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existMainConnection database error!:"), DLT_INT(eCode))
3271     }
3272     sqlite3_finalize(query);
3273     return returnVal;
3274 }
3275
3276 bool DatabaseHandler::existConnectionID(const am_connectionID_t connectionID)
3277 {
3278     sqlite3_stmt* query = NULL;
3279     std::string command = "SELECT connectionID FROM " + std::string(CONNECTION_TABLE) + " WHERE connectionID=? AND reserved=0";
3280     int eCode = 0;
3281     bool returnVal = true;
3282     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3283     sqlite3_bind_int(query, 1, connectionID);
3284     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3285         returnVal = false;
3286     else if (eCode != SQLITE_ROW)
3287     {
3288         returnVal = false;
3289         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existMainConnection database error!:"), DLT_INT(eCode))
3290     }
3291     sqlite3_finalize(query);
3292     return returnVal;
3293 }
3294
3295 bool DatabaseHandler::existcrossFader(const am_crossfaderID_t crossfaderID) const
3296 {
3297     sqlite3_stmt* query = NULL;
3298     std::string command = "SELECT crossfaderID FROM " + std::string(CROSSFADER_TABLE) + " WHERE crossfaderID=?";
3299     int eCode = 0;
3300     bool returnVal = true;
3301     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3302     sqlite3_bind_int(query, 1, crossfaderID);
3303     if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3304         returnVal = false;
3305     else if (eCode != SQLITE_ROW)
3306     {
3307         returnVal = false;
3308         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::existMainConnection database error!:"), DLT_INT(eCode))
3309     }
3310     sqlite3_finalize(query);
3311     return returnVal;
3312 }
3313
3314 am_Error_e DatabaseHandler::getSoureState(const am_sourceID_t sourceID, am_SourceState_e & sourceState) const
3315 {
3316     assert(sourceID!=0);
3317     sqlite3_stmt* query = NULL;
3318     sourceState = SS_MIN;
3319     std::string command = "SELECT sourceState FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3320     int eCode = 0;
3321     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3322     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3323     {
3324         sourceState = (am_SourceState_e) sqlite3_column_int(query, 0);
3325     }
3326     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3327     {
3328         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSoureState database error!:"), DLT_INT(eCode))
3329
3330     }
3331     sqlite3_finalize(query);
3332     return E_OK;
3333 }
3334
3335 am_Error_e DatabaseHandler::changeSourceState(const am_sourceID_t sourceID, const am_SourceState_e sourceState)
3336 {
3337     assert(sourceID!=0);
3338     sqlite3_stmt* query = NULL;
3339     std::string command = "UPDATE " + std::string(SOURCE_TABLE) + " SET sourceState=? WHERE sourceID=" + i2s(sourceID);
3340     int eCode = 0;
3341     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3342     sqlite3_bind_int(query, 1, sourceState);
3343     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3344     {
3345         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceState SQLITE Step error code:"), DLT_INT(eCode));
3346         return E_DATABASE_ERROR;
3347     }
3348
3349     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3350     {
3351         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceState SQLITE Finalize error code:"), DLT_INT(eCode));
3352         return E_DATABASE_ERROR;
3353     }
3354     return E_OK;
3355 }
3356
3357 am_Error_e DatabaseHandler::getSinkVolume(const am_sinkID_t sinkID, am_volume_t & volume) const
3358 {
3359     assert(sinkID!=0);
3360     sqlite3_stmt* query = NULL;
3361     volume = -1;
3362     std::string command = "SELECT volume FROM " + std::string(SINK_TABLE) + " WHERE sinkID=" + i2s(sinkID);
3363     int eCode = 0;
3364     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3365     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3366     {
3367         volume = sqlite3_column_int(query, 0);
3368     }
3369     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3370     {
3371         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkVolume database error!:"), DLT_INT(eCode))
3372
3373     }
3374     sqlite3_finalize(query);
3375     return E_OK;
3376 }
3377
3378 am_Error_e DatabaseHandler::getSourceVolume(const am_sourceID_t sourceID, am_volume_t & volume) const
3379 {
3380     assert(sourceID!=0);
3381     sqlite3_stmt* query = NULL;
3382     volume = -1;
3383     std::string command = "SELECT volume FROM " + std::string(SOURCE_TABLE) + " WHERE sourceID=" + i2s(sourceID);
3384     int eCode = 0;
3385     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3386     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3387     {
3388         volume = sqlite3_column_int(query, 0);
3389     }
3390     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3391     {
3392         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSourceVolume database error!:"), DLT_INT(eCode))
3393     }
3394     sqlite3_finalize(query);
3395     return E_OK;
3396 }
3397
3398 am_Error_e DatabaseHandler::getSinkSoundPropertyValue(const am_sinkID_t sinkID, const am_SoundPropertyType_e propertyType, uint16_t & value) const
3399 {
3400     assert(sinkID!=0);
3401     if (!existSink(sinkID))
3402         return E_DATABASE_ERROR; // todo: here we could change to non existent, but not shown in sequences
3403
3404     sqlite3_stmt* query = NULL;
3405     int eCode = 0;
3406     std::string command = "SELECT value FROM SinkSoundProperty" + i2s(sinkID) + " WHERE soundPropertyType=" + i2s(propertyType);
3407     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3408
3409     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3410     {
3411         value = sqlite3_column_int(query, 0);
3412     }
3413
3414     if (eCode != SQLITE_DONE)
3415     {
3416         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkSoundPropertyValue SQLITE error code:"), DLT_INT(eCode));
3417         return E_DATABASE_ERROR;
3418     }
3419
3420     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3421     {
3422         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkSoundPropertyValue SQLITE Finalize error code:"), DLT_INT(eCode));
3423         return E_DATABASE_ERROR;
3424     }
3425
3426     return E_OK;
3427 }
3428
3429 am_Error_e DatabaseHandler::getSourceSoundPropertyValue(const am_sourceID_t sourceID, const am_SoundPropertyType_e propertyType, uint16_t & value) const
3430 {
3431     assert(sourceID!=0);
3432     if (!existSource(sourceID))
3433         return E_DATABASE_ERROR; // todo: here we could change to non existent, but not shown in sequences
3434
3435     sqlite3_stmt* query = NULL;
3436     int eCode = 0;
3437     std::string command = "SELECT value FROM SourceSoundProperty" + i2s(sourceID) + " WHERE soundPropertyType=" + i2s(propertyType);
3438     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3439
3440     while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3441     {
3442         value = sqlite3_column_int(query, 0);
3443     }
3444
3445     if (eCode != SQLITE_DONE)
3446     {
3447         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkSoundPropertyValue SQLITE error code:"), DLT_INT(eCode));
3448         return E_DATABASE_ERROR;
3449     }
3450
3451     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3452     {
3453         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getSinkSoundPropertyValue SQLITE Finalize error code:"), DLT_INT(eCode));
3454         return E_DATABASE_ERROR;
3455     }
3456
3457     return E_OK;
3458 }
3459
3460 am_Error_e DatabaseHandler::getDomainState(const am_domainID_t domainID, am_DomainState_e state) const
3461 {
3462     assert(domainID!=0);
3463     sqlite3_stmt* query = NULL;
3464     state = DS_MIN;
3465     std::string command = "SELECT domainState FROM " + std::string(DOMAIN_TABLE) + " WHERE domainID=" + i2s(domainID);
3466     int eCode = 0;
3467     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3468     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3469     {
3470         state = (am_DomainState_e) sqlite3_column_int(query, 0);
3471     }
3472     else if ((eCode = sqlite3_step(query)) == SQLITE_DONE)
3473     {
3474         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getDomainState database error!:"), DLT_INT(eCode))
3475
3476     }
3477     sqlite3_finalize(query);
3478     return E_OK;
3479
3480 }
3481
3482 am_Error_e DatabaseHandler::peekDomain(const std::string & name, am_domainID_t & domainID)
3483 {
3484     sqlite3_stmt* query = NULL, *queryInsert = NULL;
3485     std::string command = "SELECT domainID FROM " + std::string(DOMAIN_TABLE) + " WHERE name=?";
3486     int eCode = 0, eCode1 = 0;
3487     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3488     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3489     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3490     {
3491         domainID = sqlite3_column_int(query, 0);
3492     }
3493     else if (eCode != SQLITE_DONE)
3494     {
3495         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekDomain database error!:"), DLT_INT(eCode))
3496         return E_DATABASE_ERROR;
3497     }
3498     else
3499     {
3500         command = "INSERT INTO " + std::string(DOMAIN_TABLE) + " (name,reserved) VALUES (?,?)";
3501         sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3502         sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3503         sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3504         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3505         {
3506             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekDomain SQLITE Step error code:"), DLT_INT(eCode1));
3507             return E_DATABASE_ERROR;
3508         }
3509
3510         if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3511         {
3512             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekDomain SQLITE Finalize error code:"), DLT_INT(eCode1));
3513             return E_DATABASE_ERROR;
3514         }
3515         domainID = sqlite3_last_insert_rowid(mDatabase);
3516     }
3517     sqlite3_finalize(query);
3518     return E_OK;
3519 }
3520
3521 am_Error_e DatabaseHandler::peekSink(const std::string & name, am_sinkID_t & sinkID)
3522 {
3523     sqlite3_stmt* query = NULL, *queryInsert = NULL;
3524     std::string command = "SELECT sinkID FROM " + std::string(SINK_TABLE) + " WHERE name=?";
3525     int eCode = 0, eCode1 = 0;
3526     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3527     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3528     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3529     {
3530         sinkID = sqlite3_column_int(query, 0);
3531     }
3532     else if (eCode != SQLITE_DONE)
3533     {
3534         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekSink database error!:"), DLT_INT(eCode))
3535         return E_DATABASE_ERROR;
3536     }
3537     else
3538     {
3539         if (mFirstStaticSink)
3540         {
3541             command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved,sinkID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
3542             mFirstStaticSink = false;
3543         }
3544         else
3545         {
3546             command = "INSERT INTO " + std::string(SINK_TABLE) + " (name,reserved) VALUES (?,?)";
3547         }
3548         sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3549         sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3550         sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3551         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3552         {
3553             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekSink SQLITE Step error code:"), DLT_INT(eCode1));
3554             return E_DATABASE_ERROR;
3555         }
3556
3557         if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3558         {
3559             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekDomain SQLITE Finalize error code:"), DLT_INT(eCode1));
3560             return E_DATABASE_ERROR;
3561         }
3562         sinkID = sqlite3_last_insert_rowid(mDatabase);
3563     }
3564     sqlite3_finalize(query);
3565     return E_OK;
3566 }
3567
3568 am_Error_e DatabaseHandler::peekSource(const std::string & name, am_sourceID_t & sourceID)
3569 {
3570     sqlite3_stmt* query = NULL, *queryInsert = NULL;
3571     std::string command = "SELECT sourceID FROM " + std::string(SOURCE_TABLE) + " WHERE name=?";
3572     int eCode = 0, eCode1 = 0;
3573     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3574     sqlite3_bind_text(query, 1, name.c_str(), name.size(), SQLITE_STATIC);
3575     if ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3576     {
3577         sourceID = sqlite3_column_int(query, 0);
3578     }
3579     else if (eCode != SQLITE_DONE)
3580     {
3581         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekSink database error!:"), DLT_INT(eCode))
3582         return E_DATABASE_ERROR;
3583     }
3584     else
3585     {
3586         if (mFirstStaticSource)
3587         {
3588             command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved,sourceID) VALUES (?,?," + i2s(DYNAMIC_ID_BOUNDARY) + ")";
3589             mFirstStaticSource = false;
3590         }
3591         else
3592         {
3593             command = "INSERT INTO " + std::string(SOURCE_TABLE) + " (name,reserved) VALUES (?,?)";
3594         }
3595         sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &queryInsert, NULL);
3596         sqlite3_bind_text(queryInsert, 1, name.c_str(), name.size(), SQLITE_STATIC);
3597         sqlite3_bind_int(queryInsert, 2, 1); //reservation flag
3598         if ((eCode1 = sqlite3_step(queryInsert)) != SQLITE_DONE)
3599         {
3600             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekSink SQLITE Step error code:"), DLT_INT(eCode1));
3601             return E_DATABASE_ERROR;
3602         }
3603
3604         if ((eCode1 = sqlite3_finalize(queryInsert)) != SQLITE_OK)
3605         {
3606             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::peekDomain SQLITE Finalize error code:"), DLT_INT(eCode1));
3607             return E_DATABASE_ERROR;
3608         }
3609         sourceID = sqlite3_last_insert_rowid(mDatabase);
3610     }
3611     sqlite3_finalize(query);
3612     return E_OK;
3613 }
3614
3615 am_Error_e DatabaseHandler::changeSinkVolume(const am_sinkID_t sinkID, const am_volume_t volume)
3616 {
3617     assert(sinkID!=0);
3618
3619     sqlite3_stmt* query = NULL;
3620     int eCode = 0;
3621     std::string command;
3622
3623     if (!existSink(sinkID))
3624     {
3625         return E_NON_EXISTENT;
3626     }
3627     command = "UPDATE " + std::string(SINK_TABLE) + " SET volume=? WHERE sinkID=" + i2s(sinkID);
3628     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3629     sqlite3_bind_int(query, 1, volume);
3630     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3631     {
3632         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkVolume SQLITE Step error code:"), DLT_INT(eCode));
3633         return E_DATABASE_ERROR;
3634     }
3635     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3636     {
3637         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkVolume SQLITE Finalize error code:"), DLT_INT(eCode));
3638         return E_DATABASE_ERROR;
3639     }
3640
3641     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSinkVolume changed volume of sink:"), DLT_INT(sinkID), DLT_STRING("to:"), DLT_INT(volume));
3642
3643     return E_OK;
3644 }
3645
3646 am_Error_e DatabaseHandler::changeSourceVolume(const am_sourceID_t sourceID, const am_volume_t volume)
3647 {
3648     assert(sourceID!=0);
3649
3650     sqlite3_stmt* query = NULL;
3651     int eCode = 0;
3652     std::string command;
3653
3654     if (!existSource(sourceID))
3655     {
3656         return E_NON_EXISTENT;
3657     }
3658     command = "UPDATE " + std::string(SOURCE_TABLE) + " SET volume=? WHERE sourceID=" + i2s(sourceID);
3659     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3660     sqlite3_bind_int(query, 1, volume);
3661     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3662     {
3663         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceVolume SQLITE Step error code:"), DLT_INT(eCode));
3664         return E_DATABASE_ERROR;
3665     }
3666     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3667     {
3668         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceVolume SQLITE Finalize error code:"), DLT_INT(eCode));
3669         return E_DATABASE_ERROR;
3670     }
3671
3672     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSourceVolume changed volume of source=:"), DLT_INT(sourceID), DLT_STRING("to:"), DLT_INT(volume));
3673
3674     return E_OK;
3675 }
3676
3677 am_Error_e DatabaseHandler::changeSourceSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sourceID_t sourceID)
3678 {
3679     //todo: add checks if soundproperty exists!
3680     assert(sourceID!=0);
3681
3682     sqlite3_stmt* query = NULL;
3683     int eCode = 0;
3684     std::string command;
3685
3686     if (!existSource(sourceID))
3687     {
3688         return E_NON_EXISTENT;
3689     }
3690     command = "UPDATE SourceSoundProperty" + i2s(sourceID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
3691     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3692     sqlite3_bind_int(query, 1, soundProperty.value);
3693     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3694     {
3695         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceSoundPropertyDB SQLITE Step error code:"), DLT_INT(eCode));
3696         return E_DATABASE_ERROR;
3697     }
3698
3699     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3700     {
3701         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSourceSoundPropertyDB SQLITE Finalize error code:"), DLT_INT(eCode));
3702         return E_DATABASE_ERROR;
3703     }
3704
3705     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSourceSoundPropertyDB changed SourceSoundProperty of source:"), DLT_INT(sourceID), DLT_STRING("type:"), DLT_INT(soundProperty.type), DLT_STRING("to:"), DLT_INT(soundProperty.value));
3706
3707     return E_OK;
3708 }
3709
3710 am_Error_e DatabaseHandler::changeSinkSoundPropertyDB(const am_SoundProperty_s & soundProperty, const am_sinkID_t sinkID)
3711 {
3712     //todo: add checks if soundproperty exists!
3713     assert(sinkID!=0);
3714
3715     sqlite3_stmt* query = NULL;
3716     int eCode = 0;
3717     std::string command;
3718
3719     if (!existSink(sinkID))
3720     {
3721         return E_NON_EXISTENT;
3722     }
3723     command = "UPDATE SinkSoundProperty" + i2s(sinkID) + " SET value=? WHERE soundPropertyType=" + i2s(soundProperty.type);
3724     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3725     sqlite3_bind_int(query, 1, soundProperty.value);
3726     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3727     {
3728         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkSoundPropertyDB SQLITE Step error code:"), DLT_INT(eCode));
3729         return E_DATABASE_ERROR;
3730     }assert(sinkID!=0);
3731
3732     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3733     {
3734         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeSinkSoundPropertyDB SQLITE Finalize error code:"), DLT_INT(eCode));
3735         return E_DATABASE_ERROR;
3736     }
3737
3738     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeSinkSoundPropertyDB changed MainSinkSoundProperty of sink:"), DLT_INT(sinkID), DLT_STRING("type:"), DLT_INT(soundProperty.type), DLT_STRING("to:"), DLT_INT(soundProperty.value));
3739
3740     return E_OK;
3741 }
3742
3743 am_Error_e DatabaseHandler::changeCrossFaderHotSink(const am_crossfaderID_t crossfaderID, const am_HotSink_e hotsink)
3744 {
3745     assert(crossfaderID!=0);
3746
3747     sqlite3_stmt* query = NULL;
3748     int eCode = 0;
3749     std::string command;
3750
3751     if (!existcrossFader(crossfaderID))
3752     {
3753         return E_NON_EXISTENT;
3754     }
3755     command = "UPDATE " + std::string(CROSSFADER_TABLE) + " SET hotsink=? WHERE crossfaderID=" + i2s(crossfaderID);
3756     sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3757     sqlite3_bind_int(query, 1, hotsink);
3758     if ((eCode = sqlite3_step(query)) != SQLITE_DONE)
3759     {
3760         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeCrossFaderHotSink SQLITE Step error code:"), DLT_INT(eCode));
3761         return E_DATABASE_ERROR;
3762     }
3763     if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3764     {
3765         DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::changeCrossFaderHotSink SQLITE Finalize error code:"), DLT_INT(eCode));
3766         return E_DATABASE_ERROR;
3767     }
3768
3769     DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DatabaseHandler::changeCrossFaderHotSink changed hotsink of crossfader="), DLT_INT(crossfaderID), DLT_STRING("to:"), DLT_INT(hotsink));
3770
3771     return E_OK;
3772 }
3773
3774 am_Error_e DatabaseHandler::getRoutingTree(bool onlyfree, RoutingTree& tree, std::vector<RoutingTreeItem*>& flatTree)
3775 {
3776     sqlite3_stmt* query = NULL;
3777     int eCode = 0;
3778     size_t i = 0;
3779     std::string command;
3780     am_domainID_t rootID = tree.returnRootDomainID();
3781     RoutingTreeItem *parent = tree.returnRootItem();
3782
3783     command = "SELECT domainSourceID,gatewayID FROM " + std::string(GATEWAY_TABLE) + " WHERE domainSinkID=? AND inUse=?";
3784
3785     do
3786     {
3787         if (i != 0)
3788         {
3789             parent = flatTree.at(i - 1);
3790             rootID = parent->returnDomainID();
3791         }
3792         sqlite3_prepare_v2(mDatabase, command.c_str(), -1, &query, NULL);
3793         sqlite3_bind_int(query, 1, rootID);
3794         sqlite3_bind_int(query, 2, onlyfree);
3795
3796         while ((eCode = sqlite3_step(query)) == SQLITE_ROW)
3797         {
3798             flatTree.push_back(tree.insertItem(sqlite3_column_int(query, 0), sqlite3_column_int(query, 1), parent));
3799         }
3800
3801         if (eCode != SQLITE_DONE)
3802         {
3803             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getRoutingTree SQLITE error code:"), DLT_INT(eCode));
3804             return (E_DATABASE_ERROR);
3805         }
3806
3807         if ((eCode = sqlite3_finalize(query)) != SQLITE_OK)
3808         {
3809             DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DatabaseHandler::getRoutingTree SQLITE Finalize error code:"), DLT_INT(eCode));
3810             return (E_DATABASE_ERROR);
3811         }
3812         i++;
3813     } while (flatTree.size() > (i - 1));
3814
3815     return (E_OK);
3816 }
3817
3818 void DatabaseHandler::createTables()
3819 {
3820     for (uint16_t i = 0; i < sizeof(databaseTables) / sizeof(databaseTables[0]); i++)
3821     {
3822         assert(sqQuery("CREATE TABLE " + databaseTables[i]));
3823     }
3824 }
3825