update on dbus
[profile/ivi/audiomanager.git] / AudioManagerDaemon / DataBaseHandler.h
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * AudioManangerDeamon
5  *
6  * \file DataBaseHandler.h
7  *
8  * \date 20.05.2011
9  * \author Christian Müller (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 Müller  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
26 #ifndef DATABASEHANDLER_H_
27 #define DATABASEHANDLER_H_
28
29 #include <string>
30 #include <sqlite3.h>
31
32 #define AUDIO_DATABASE "audiomanagerDB.sqlite"
33
34 #define DOMAIN_TABLE "Domains"
35 #define SOURCE_CLASS_TABLE "SourceClasses"
36 #define SINK_CLASS_TABLE "SinkClasses"
37 #define SOURCE_TABLE "Sources"
38 #define SINK_TABLE "Sinks"
39 #define GATEWAY_TABLE "Gateways"
40 #define CONNECTION_TABLE "Connections"
41 #define INTERRUPT_TABLE "Interrupts"
42 #define MAIN_TABLE "MainTable"
43
44 #define SQexecute(command) sqlite3_prepare_v2(m_database,command.c_str(),-1,&query,NULL)== SQLITE_OK
45
46 #include "audioManagerIncludes.h"
47
48 class RoutingTreeItem;
49 class Router;
50 class SinkType;
51 class SourceType;
52 class ConnectionType;
53 class RoutingTree;
54
55 /**
56  * \class DataBaseHandler
57  * \brief The handler of the database
58  * \details creates, deletes inserts and reads data in the database. Works on top of SQLite
59  *
60  *\fn bool DataBaseHandler::open_database()
61  * \brief opens the database
62  *
63  * \fn DataBaseHandler::close_database()
64  * \brief closes the database
65  *
66  * \fn bool DataBaseHandler::delete_data(std::string table)
67  * \brief deletes the data of a complete table
68  * \param table the name of the table to be deleted
69  *
70  * \fn bool DataBaseHandler::create_tables()
71  * \brief creates all neccessary tables
72  *
73  * \fn domain_t DataBaseHandler::insert_into_Domains_table(std::string DomainName, std::string BusName, std::string NodeName, bool EarlyMode)
74  * \brief inserts data into the domain table
75  * \param DomainName the name to be inserted
76  * \param BusName the name of the Bus that is used for the domain
77  * \param NodeName the name of the RoutingAdaptor that belongs to the domain
78  * \param EarlyMode true if the domain is an early domain
79  * \return the domain id (unique)
80  *
81  * \fn sourceClass_t DataBaseHandler::insert_into_Source_Class_table(std::string ClassName, volume_t VolumeOffset, bool IsInterrupt, bool IsMixed)
82  * \brief inserts data into the Source Class table
83  * \param ClassName name of the class
84  * \param VolumeOffset the volume offset of the class
85  * \param IsInterrupt true if it is an interrupt
86  * \param IsMixed true if it is mixed
87  * \return the unique Class ID
88  *
89  * \fn sinkClass_t DataBaseHandler::insert_into_Sink_Class_table(std::string ClassName)
90  * \brief enters new Sink Class in the Class Table
91  * \param ClassName the name of the class
92  * \return unique class ID
93  *
94  * \fn source_t DataBaseHandler::insert_into_Source_table(std::string Name, sourceClass_t Class_ID, domain_t Domain_ID, bool IsGateway)
95  * \brief inserts a new source into the source table
96  * \param Name the name of the source
97  * \param Class_ID the class ID of the source
98  * \param Domain_ID the domain ID
99  * \param IsGateway true if it is a gateway
100  * \return returns a new source ID
101  *
102  * \fn sink_t DataBaseHandler::insert_into_Sink_table(std::string Name, sinkClass_t Class_ID, domain_t Domain_ID, bool IsGateway)
103  * \brief inserts a new Sink into the table
104  * \param Name the name of the sink
105  * \param Class_ID the class ID
106  * \param Domain_ID the domain ID
107  * \param IsGateway true if it is a gateway
108  * \return the new Sink ID
109  *
110  * \fn gateway_t DataBaseHandler::insert_into_Gatway_table(std::string Name, sink_t Sink_ID, source_t Source_ID, domain_t DomainSource_ID, domain_t DomainSink_ID, domain_t ControlDomain_ID)
111  * \brief inserts a gateway into the database
112  * \param Name the name of the gateway
113  * \param Sink_ID the sink id of the gateway
114  * \param Source_ID the source id of the gateway
115  * \param DomainSource_ID the domain id where the source is in
116  * \param DomainSink_ID the domain id where the sinḱ is in
117  * \param ControlDomain_ID the domain which controls the gateway
118  * \return the unique id of the gateway
119  *
120  * \fn domain_t DataBaseHandler::peek_Domain_ID(std::string DomainName)
121  * \brief reserves a domain ID but does not register it.
122  * \details This function is used to register Gateways. The problem is that a gateway is registered from one domain that does not know if the other end of the gateway domain is already registered.
123  * this can be solved via peeking a domain ID. By using this function the domain is only "reserved" not registered.
124  * \param DomainName the name of the domain to be peeked
125  * \return the Domain ID
126  *
127  * \fn domain_t DataBaseHandler::get_Domain_ID_from_Source_ID(source_t Source_ID)
128  * \brief returns the domain if from a source given via an Id
129  * \param Source_ID the source that the domain shall be returned of
130  * \return the domain ID
131  *
132  * \fn domain_t DataBaseHandler::get_Domain_ID_from_Sink_ID(sink_t Sink_ID)
133  * \brief returns the domain from a given sink ID
134  * \param Sink_ID the sink ID
135  * \return the domain ID
136  *
137  * \fn source_t DataBaseHandler::get_Source_ID_from_Name(std::string name)
138  * \brief returns the source ID from a name
139  * \param name the name for witch the source ID shall be returned
140  * \return the source ID
141  *
142  * \fn sourceClass_t DataBaseHandler::get_Source_Class_ID_from_Name(std::string name)
143  * \brief returns the source class ID from a given class name
144  * \return the source class ID
145  *
146  * \fn domain_t DataBaseHandler::get_Domain_ID_from_Name(std::string name)
147  * \brief returns the domain ID from given domain name
148  * \param name the name
149  * \return the domain ID
150  *
151  * \fn sink_t DataBaseHandler::get_Sink_ID_from_Name(std::string name)
152  * \brief returns the Sink ID from a given name
153  * \param name the name
154  * \return the sink ID
155  *
156  * \fn std::string DataBaseHandler::get_Bus_from_Domain_ID(domain_t Domain_ID)
157  * \brief returns the bus name for a given domain ID
158  * \param Domain_ID the domain ID
159  * \return the name of the bus
160  *
161  * \fn domain_t DataBaseHandler::get_Domain_ID_from_Connection_ID(connection_t ID)
162  * \brief returns the domain ID from a given Connection ID
163  * \param ID the connection ID
164  * \return the domain ID
165  *
166  * \fn bool DataBaseHandler::is_source_Mixed(source_t source)
167  * \brief is used to find out if a source is mixed. Used for interrrupt sources
168  * \param source the source
169  * \return true if source is mixed.
170  *
171  * \fn gateway_t DataBaseHandler::get_Gateway_ID_with_Domain_ID(domain_t startDomain_ID, domain_t targetDomain_ID)
172  * \brief returns a gateway ID for a given domain connection
173  * \param  startDomain_ID the domain ID where the gateway should start
174  * \param targetDomain_ID the domain ID where the gateway should stop
175  * \return the gateway ID
176  *
177  * \fn genError_t DataBaseHandler::get_Gateway_Source_Sink_Domain_ID_from_ID(gateway_t Gateway_ID,source_t* return_Source_ID,sink_t* return_Sink_ID,domain_t* return_ControlDomain_ID)
178  * \brief retruns informations about gateways
179  * \param Gateway_ID the gateways ID as input
180  * \param return_Source_ID call by reference source ID return value
181  * \param return_Sink_ID call by reference Sink ID return value
182  * \param return_ControlDomain_ID call by reference Control Domain ID return value
183  *
184  * \fn void DataBaseHandler::get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree, std::list<RoutingTreeItem*>* allItems)
185  * \brief is used to create a Tree out of RoutingTreeItems. Used for routing algorithm
186  * \param onlyfree if called with true then only routes via free gateways will be returned
187  * \param Tree pointer the the Routing Tree
188  * \param allItems pointer to a list of all Routing Tree Items as call by reference value
189  *
190  * \fn void DataBaseHandler::getListofSources(std::list<SourceType>* SourceList)
191  * \brief returns a list of all sources
192  * \param SourceList call by reference pointer to return value
193  *
194  * \fn void DataBaseHandler::getListofSinks(std::list<SinkType>* SinkList)
195  * \brief returns a list of all sinks
196  * \param SinkList call by reference pointer to return value
197  *
198  * \fn void DataBaseHandler::getListofConnections(std::list<ConnectionType>* ConnectionList)
199  * \brief returns a list of all connections
200  * \param ConnectionList call by reference pointer to return value
201  *
202  * \fn connection_t DataBaseHandler::getConnectionID(source_t SourceID,sink_t SinkID)
203  * \brief returns the connection ID for a given source sink combination
204  * \param SourceID the source ID
205  * \param SinkID the sink ID
206  * \return the connection ID, -1 if there is no connection
207  *
208  * \fn connection_t DataBaseHandler::insertConnection(source_t SourceID,sink_t SinkID)
209  * \brief inserts a connection into the database
210  * \param SourceID the source ID
211  * \param SinkID the sink ID
212  * \return the connection ID of the newly entered connection
213  *
214  * \fn genError_t DataBaseHandler::removeConnection(connection_t ConnectionID)
215  * \brief removes a connection from the database
216  * \param ConnectionID the connection ID
217  * \return GEN_OK if everything was ok
218  *
219  * \fn connection_t DataBaseHandler::reserveMainConnection(source_t source,sink_t sink)
220  * \brief reserves a main connection ID
221  * \param source the source
222  * \param sink the sink
223  * \return the main connection ID of the newly entered main connection
224  *
225  * \fn genError_t DataBaseHandler::updateMainConnection(connection_t connID,genRoute_t route)
226  * \brief updates the main connection. Enters the missing information into the main connection that was reserved before
227  * \param connID connection ID
228  * \param route the route information to be entered
229  * \return GEN_OK on success
230  *
231  * \fn genError_t DataBaseHandler::getMainConnectionDatafromID(const connection_t connID, sink_t* return_sinkID,source_t* return_sourceID,genRoute_t** return_route)
232  * \brief returns details for a connection ID
233  * \param connID the connection ID
234  * \param return_sinkID call by reference pointer to return Sink ID
235  * \param return_sourceID call by reference pointer to return Source ID
236  * \param return_route call by reference pointer to return pointer of the route
237  * \return GEN_OK on success
238  *
239  * \fn connection_t DataBaseHandler::returnMainconnectionIDforSinkSourceID (sink_t sink, source_t source)
240  * \brief return a main connection ID for a sink source combination
241  * \param sink the sink ID
242  * \param source the source ID
243  * \return the connection ID
244  *
245  * \fn std::list<source_t> DataBaseHandler::getSourceIDsForSinkID (sink_t sink)
246  * \brief returns the list of source IDs that are connected to a sink ID
247  * \param sink the sink ID
248  * \return a list of source IDs
249  *
250  * \fn std::list<ConnectionType> DataBaseHandler::getListAllMainConnections()
251  * \brief returns a list of all mainconnections
252  * \return a list of all connections
253  *
254  * \fn genError_t DataBaseHandler::removeMainConnection(connection_t connID)
255  * \brief removes a main connection ID
256  * \param connID the connection ID to be removed
257  * \return GEN_OK on success
258  *
259  * \fn genInt_t DataBaseHandler::reserveInterrupt(sink_t Sink_ID, source_t Source_ID)
260  * \brief reserve an interrupt
261  * \details use this to reserve an interrupt ID before the actual connections is build up. Via DataBaseHandler::updateInterrupt the information can be complemented afterwards
262  * \param Sink_ID the sink ID
263  * \param Source_ID the source ID
264  * \return the interrupt ID
265  *
266  * \fn genError_t DataBaseHandler::updateInterrupt(const genInt_t intID,connection_t connID, bool mixed, std::list<source_t> listInterrruptedSources)
267  * \brief use this to enter the missing information into a reserved Interrupt into the database
268  * \param intID the interrupt ID
269  * \param connID the connection ID that is used
270  * \param mixed true if the interrupt is mixed
271  * \param listInterrruptedSources the list of interrupted sources. Used to restore the old state afterwards
272  * \return GEN_OK on success
273  *
274  * \fn genError_t DataBaseHandler::getInterruptDatafromID(const genInt_t intID, connection_t* return_connID, sink_t* return_Sink_ID, source_t* return_Source_ID, bool* return_mixed, std::list<source_t>** return_listInterrruptedSources)
275  * \brief returns information about interrupts from the ID
276  * \param intID the interrupt ID
277  * \param return_connID pointer to call by reference connection ID
278  * \param return_Sink_ID pointer to call by reference sink ID
279  * \param return_Source_ID pointer to call by reference source ID
280  * \param return_mixed pointer to call by reference mixed value
281  * \param return_listInterrruptedSources pointer to call by reference list of interrupted sources
282  * \return GEN_OK on success
283  *
284  * \fn genError_t DataBaseHandler::removeInterrupt(const genInt_t intID)
285  * \brief removes an interrupt from the database
286  * \param intID the interrrupt ID to be removed
287  * \return GEN_OK on success
288  *
289  *      \fn void DataBaseHandler::signal_connectionChanged()
290  *      \brief this signal is emitted when connections are changed
291  *
292  *      \fn void DataBaseHandler::signal_numberOfSinksChanged()
293  *      \brief this signal is emitted when the number of sinks changed
294  *
295  *      \fn void DataBaseHandler::signal_numberOfSourcesChanged()
296  *      \brief this signal is emitted when the number of sources changed
297  *
298  */
299
300 class DataBaseHandler {
301
302 public:
303         DataBaseHandler();
304         virtual ~DataBaseHandler();
305
306         bool open_database();
307         void close_database();
308         bool delete_data(std::string table);
309         bool create_tables();
310
311         domain_t insert_into_Domains_table(std::string DomainName, std::string BusName,
312                         std::string NodeName, bool EarlyMode);
313         sourceClass_t insert_into_Source_Class_table(std::string ClassName,
314                         volume_t VolumeOffset, bool IsInterrupt, bool IsMixed);
315         sinkClass_t insert_into_Sink_Class_table(std::string ClassName);
316         source_t insert_into_Source_table(std::string Name, sourceClass_t Class_ID,
317                         domain_t Domain_ID, bool IsGateway);
318         sink_t insert_into_Sink_table(std::string Name, sinkClass_t Class_ID,
319                         domain_t Domain_ID, bool IsGateway);
320         gateway_t insert_into_Gatway_table(std::string Name, sink_t Sink_ID,
321                         source_t Source_ID, domain_t DomainSource_ID,
322                         domain_t DomainSink_ID, domain_t ControlDomain_ID);
323
324         domain_t peek_Domain_ID(std::string DomainName);
325         domain_t get_Domain_ID_from_Source_ID(source_t Source_ID);
326         domain_t get_Domain_ID_from_Sink_ID(sink_t Sink_ID);
327
328         source_t get_Source_ID_from_Name(std::string name);
329         sourceClass_t get_Source_Class_ID_from_Name(std::string name);
330         domain_t get_Domain_ID_from_Name(std::string name);
331         sink_t get_Sink_ID_from_Name(std::string name);
332         std::string get_Bus_from_Domain_ID(domain_t Domain_ID);
333         domain_t get_Domain_ID_from_Connection_ID(connection_t ID);
334
335         bool is_source_Mixed(source_t source);
336
337         gateway_t get_Gateway_ID_with_Domain_ID(domain_t startDomain_ID,
338                         domain_t targetDomain_ID);
339         genError_t get_Gateway_Source_Sink_Domain_ID_from_ID(gateway_t Gateway_ID,
340                         source_t* return_Source_ID, sink_t* return_Sink_ID,
341                         domain_t* return_ControlDomain_ID);
342         void get_Domain_ID_Tree(bool onlyfree, RoutingTree* Tree,
343                         std::list<RoutingTreeItem*>* allItems);
344
345         void getListofSources(std::list<SourceType>* SourceList);
346         void getListofSinks(std::list<SinkType>* SinkList);
347         void getListofConnections(std::list<ConnectionType>* ConnectionList);
348
349         connection_t getConnectionID(source_t SourceID, sink_t SinkID);
350         connection_t insertConnection(source_t SourceID, sink_t SinkID);
351         genError_t removeConnection(connection_t ConnectionID);
352         connection_t reserveMainConnection(source_t source, sink_t sink);
353         genError_t updateMainConnection(connection_t connID, genRoute_t route);
354         genError_t getMainConnectionDatafromID(const connection_t connID,
355                         sink_t* return_sinkID, source_t* return_sourceID,
356                         genRoute_t** return_route);
357         connection_t returnMainconnectionIDforSinkSourceID(sink_t sink,
358                         source_t source);
359         std::list<source_t> getSourceIDsForSinkID(sink_t sink);
360         std::list<ConnectionType> getListAllMainConnections();
361         genError_t removeMainConnection(connection_t connID);
362         genInt_t reserveInterrupt(sink_t Sink_ID, source_t Source_ID);
363         genError_t updateInterrupt(const genInt_t intID, connection_t connID,
364                         bool mixed, std::list<source_t> listInterrruptedSources);
365         genError_t getInterruptDatafromID(const genInt_t intID,
366                         connection_t* return_connID, sink_t* return_Sink_ID,
367                         source_t* return_Source_ID, bool* return_mixed,
368                         std::list<source_t>** return_listInterrruptedSources);
369         genError_t removeInterrupt(const genInt_t intID);
370
371         void signal_connectionChanged();
372         void signal_numberOfSinksChanged();
373         void signal_numberOfSourcesChanged();
374
375 private:
376         bool pQuery(std::string command);
377         std::string int2string(int i);
378         sqlite3 *m_database; //!< pointer to database
379         std::string m_path;
380 };
381
382 #endif /* DATABASEHANDLER_H_ */