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