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