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