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