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