36d86b5d37e3972865fb280ad7275b9c5ba95d1e
[profile/ivi/audiomanager.git] / AudioManagerDaemon / AudioManagerCore.h
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * AudioManangerDeamon
5  *
6  * \file AudioManagerCore.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 AUDIOMANAGERCORE_H_
27 #define AUDIOMANAGERCORE_H_
28
29 #include <QtCore/QObject>
30
31 #include "audioManagerIncludes.h"
32
33 /**CONSTANT for the Connection timeout when using asynchronous connect. In mseconds.
34  *
35  */
36 #define CONNECT_TIMEOUT 3000
37
38 class HookHandler;
39 class AudioManagerCore;
40 class Queue;
41 class Router;
42 class Bushandler;
43 class DBusCommandInterface;
44
45 /**
46  * \class Task
47  * \brief Base Class for all Tasks
48  * \details a Base Class for a basic operation together with a set of parameters
49  * Every Task that can be created by the Hook Plugins is derived from this class
50  * Be sure to implement executeTask() because this will be called when the task is executed
51  * Whenever the task is finished, you need to emit the signal signal_nextTask() that will call the next
52  * Task in the queue.
53  * \fn virtual void Task::executeTask(Queue* queue)=0
54  * \brief This needs to be overwritten in implementation of the class
55  * The queue calls this function to start the execution of the task
56  * \param queue pointer to the queue
57  * \fn void Task::signal_nextTask()
58  * \brief by emitting this signal, the next task in the queue is triggered
59  */
60 class Task: public QObject {
61 Q_OBJECT
62 public:
63         Task();
64         Task(AudioManagerCore* core);
65         virtual ~Task();
66         virtual void executeTask(Queue* queue)=0;
67
68 signals:
69         void signal_nextTask();
70 protected:
71         AudioManagerCore* m_core; //!< pointer to the core
72 };
73
74 /**
75  * \class TaskAsyncConnect
76  * \brief This task is used to connect a source and a sink asynchronous
77  *
78  * \fn TaskAsyncConnect::TaskAsyncConnect(AudioManagerCore* core, sink_t sink, source_t source)
79  * \brief Constructor
80  * \param core pointer to the core
81  * \param sink  the sink to be connected
82  * \param source the source to be connected
83  *
84  * \fn void TaskAsyncConnect::setSink(sink_t sink)
85  * \brief Sets the Sink that should be connected to
86  * \param sink the sink
87  *
88  * \fn void TaskAsyncConnect::setSource(source_t source)
89  * \brief sets the source
90  * \param source the source
91  *
92  * \fn sink_t TaskAsyncConnect::getSink()
93  * \brief returns the sink
94  *
95  * \fn source_t TaskAsyncConnect::getSource()
96  * \brief returns the source
97  *
98  * \fn void TaskAsyncConnect::slot_connect_finished(genHandle_t handle, genError_t error)
99  * \brief is used to receive the asynchronous answer.
100  *
101  * \fn void TaskAsyncConnect::slot_timeout()
102  * \brief used to handle the timeout
103  */
104 class TaskAsyncConnect: public Task {
105 Q_OBJECT
106 public:
107         TaskAsyncConnect(AudioManagerCore* core, sink_t sink, source_t source);
108         virtual ~TaskAsyncConnect();
109         void setSink(sink_t sink);
110         void setSource(source_t source);
111         sink_t getSink();
112         source_t getSource();
113         void executeTask(Queue* queue);
114
115 public slots:
116         void slot_connect_finished(genHandle_t handle, genError_t error);
117         void slot_timeout();
118 private:
119         sink_t m_ParamSink; //!< the sink to be connected
120         source_t m_ParamSource; //!< the source to be connected
121         genHandle_t m_handle; //!< the handle (ConnectionID)
122         QTimer* m_timer; //!< pointer to a timer used for timeout tracking
123 };
124
125 /**
126  * \class TaskConnect
127  * \brief This task is used to connect a source and a sink synchronous
128  *
129  * \fn TaskConnect::TaskConnect(AudioManagerCore* core, sink_t sink, source_t source)
130  * \brief constructor
131  * \param core pointer to the core
132  * \param sink  the sink to be connected
133  * \param source the source to be connected
134  *
135  */
136 class TaskConnect: public Task {
137 Q_OBJECT
138 public:
139         TaskConnect(AudioManagerCore* core, sink_t sink, source_t source);
140         virtual ~TaskConnect();
141         void executeTask(Queue* queue);
142
143 private:
144         sink_t m_ParamSink; //!< the sink to be connected
145         source_t m_ParamSource; //!< the source to be connected
146 };
147
148 /**
149  * \class TaskDisconnect
150  * \brief This task is used to connect a source and a sink synchronous
151  *
152  * \fn TaskDisconnect::TaskDisconnect(AudioManagerCore* core, connection_t ID)
153  * \brief constructor
154  * \param core pointer to the core
155  * \param ID the connection ID to be disconnected
156  *
157  */
158 class TaskDisconnect: public Task {
159 Q_OBJECT
160 public:
161         TaskDisconnect(AudioManagerCore* core, connection_t ID);
162         virtual ~TaskDisconnect();
163         void executeTask(Queue* queue);
164 private:
165         connection_t m_ParamConnectionID; //!< the connection to disconnect
166 };
167
168 /**
169  * \class TaskInterruptWait
170  * \brief this class waits for an interrupt to be resumed
171  *
172  * \fn TaskInterruptWait::TaskInterruptWait(AudioManagerCore* core, genInt_t connectionID)
173  * \brief Constructor
174  * \param core link to AudioManagercore
175  * \param connectionID the connection ID to be waited for
176  *
177  * \fn void TaskInterruptWait::slot_interrupt_ready(genInt_t ID)
178  * \brief slot that is called when the interrupt resumes
179  * \param the interrupt ID of the interrupt that resumed
180  */
181 class TaskInterruptWait: public Task {
182 Q_OBJECT
183 public:
184         TaskInterruptWait(AudioManagerCore* core, genInt_t connectionID);
185         virtual ~TaskInterruptWait();
186         void executeTask(Queue* queue);
187 public slots:
188         void slot_interrupt_ready(genInt_t ID);
189 private:
190         genInt_t m_interruptID; //!< the interrupt ID that should be waited for
191 };
192
193 /**
194  * \class TaskSetVolume
195  * \brief a task that sets the volume for a sink
196  *
197  * \fn TaskSetVolume::TaskSetVolume(AudioManagerCore* core, volume_t newVolume=0, sink_t sink=0)
198  * \brief constructor
199  * \param core the pointer to the AudioManagerCore
200  * \param newVolume the volume to be set
201  * \param sink the sink that the volume shall be applied to
202  *
203  * \fn void TaskSetVolume::setVolume(volume_t newVolume)
204  * \brief sets the volume
205  *
206  * \fn void TaskSetVolume::setSink(sink_t sink)
207  * \brief sets the sink
208  *
209  * \fn void TaskSetVolume::volume_t getVolume()
210  * \brief returns the volume
211  *
212  * \fn sink_t TaskSetVolume::getSink()
213  * \brief returns the volume
214  */
215 class TaskSetVolume: public Task {
216 Q_OBJECT
217 public:
218         TaskSetVolume(AudioManagerCore* core, volume_t newVolume=0, sink_t sink=0);
219         virtual ~TaskSetVolume();
220
221         void setVolume(volume_t newVolume);
222         void setSink(sink_t sink);
223
224         volume_t getVolume();
225         sink_t getSink();
226
227         void executeTask(Queue* queue);
228 private:
229         volume_t m_volume; //!< the volume
230         sink_t m_sink; //!< the sink
231 };
232
233 /**
234  * \class TaskSetSourceVolume
235  * \brief sets the volume of a source
236  *
237  * \fn TaskSetSourceVolume::TaskSetSourceVolume(AudioManagerCore* core, volume_t newVolume=0, source_t source=0)
238  * \brief constructor
239  * \param core the pointer to the AudioManagerCore
240  * \param newVolume the volumesink_t  to be set
241  * \param source the source that the volume shall be applied to
242  *
243  *  * \fn void TaskSetSourceVolume::setVolume(volume_t newVolume)
244  * \brief sets the volume
245  *
246  * \fn void TaskSetSourceVolume::setSink(sink_t sink)
247  * \brief sets the sink
248  *
249  * \fn void TaskSetSourceVolume::volume_t getVolume()
250  * \brief returns the volume
251  *
252  * \fn sink_t TaskSetSourceVolume::getSink()
253  * \brief returns the volume
254  *
255  */
256 class TaskSetSourceVolume: public Task {
257 Q_OBJECT
258 public:
259         TaskSetSourceVolume(AudioManagerCore* core, volume_t newVolume=0, source_t source=0);
260         virtual ~TaskSetSourceVolume();
261
262         void setVolume(volume_t newVolume);
263         void setSource(source_t source);
264
265         volume_t getVolume();
266         source_t getSource();
267
268         void executeTask(Queue* queue);
269 private:
270         volume_t m_volume; //!< the volume
271         source_t m_source; //!< the source
272 };
273
274 /**
275  * \class TaskWait
276  * \brief implements a wait in msecons
277  *
278  * \fn TaskWait::TaskWait(AudioManagerCore* core, int mseconds)
279  * \brief constructor
280  * \param core the pointer to the AudioManagerCore
281  * \param newmseconds delay in milliseconds
282  *
283  * \fn void TaskWait::setTime(int mseconds)
284  * \brief sets the time
285  *
286  * \fn int TaskWait::getTime()
287  * \brief returns the time
288  *
289  * \fn  void TaskWait::slot_timeIsup()
290  * \brief slot called when the time is up
291  */
292 class TaskWait: public Task {
293 Q_OBJECT
294 public:
295         TaskWait(AudioManagerCore* core, int mseconds);
296         virtual ~TaskWait();
297         void setTime(int mseconds);
298         int getTime();
299         void executeTask(Queue* queue);
300
301 public slots:
302         void slot_timeIsup();
303 private:
304         int m_ParamMSeconds;//!< time to be delayed in milli seconds
305         QTimer* m_timer; //!< pointer to timer
306 };
307
308 /**
309  * \class TaskEnterUserConnect
310  * \brief This task enters the user connection into the struct of the AudioManagerCore
311  * \details We need this because the connection shall be entered not before it is build up
312  *
313  * \fn TaskEnterUserConnect::TaskEnterUserConnect(AudioManagerCore* core, genRoute_t route, connection_t connID=0)
314  * \brief enters a user connect into the database
315  */
316 class TaskEnterUserConnect: public Task {
317 Q_OBJECT
318 public:
319         TaskEnterUserConnect(AudioManagerCore* core, genRoute_t route, connection_t connID=0);
320         virtual ~TaskEnterUserConnect();
321         void setConnection(genRoute_t route, connection_t connID);
322         genRoute_t returnConnection();
323         void executeTask(Queue* queue);
324 private:
325         genRoute_t m_route;             //!< the route to be entered
326         connection_t m_connectionID; //!< the connection ID
327 };
328
329 /**
330  * \class TaskRemoveUserConnect
331  * \brief removes a user Connect
332  *
333  * \fn TaskRemoveUserConnect::TaskRemoveUserConnect(AudioManagerCore* core, connection_t connID)
334  * \brief constructor
335  */
336 class TaskRemoveUserConnect : public Task {
337 Q_OBJECT
338 public:
339         TaskRemoveUserConnect(AudioManagerCore* core, connection_t connID);
340         virtual ~TaskRemoveUserConnect();
341         void setConnectionID(connection_t connID);
342         connection_t returnConnectionID();
343         void executeTask(Queue* queue);
344 private:
345         connection_t m_connectionID; //!< the connection ID
346 };
347
348 /**
349  * \class TaskEnterInterrupt
350  * \brief enters an interrupt into the database
351  */
352 class TaskEnterInterrupt: public Task {
353 Q_OBJECT
354 public:
355         TaskEnterInterrupt(AudioManagerCore* core, genInt_t ID,bool mixed, connection_t connID, QList<source_t> listInterruptedSources);
356         virtual ~TaskEnterInterrupt();
357         void executeTask(Queue* queue);
358 private:
359         genInt_t m_intID; //!< the interrupt ID
360         bool m_mixed; //!< true if mixed
361         connection_t m_connectionID; //!< the connection ID
362         QList<source_t> m_interruptedSourcesList; //!< the list of interrupted sources
363 };
364
365 /**
366  * \class TaskRemoveInterrupt
367  * \brief removes an interrupt
368  */
369 class TaskRemoveInterrupt: public Task {
370 Q_OBJECT
371 public:
372         TaskRemoveInterrupt(AudioManagerCore* core, genInt_t ID);
373         virtual ~TaskRemoveInterrupt();
374         void executeTask(Queue* queue);
375 private:
376         genInt_t m_intID; //!< the interrupt ID
377 };
378
379 /**
380  * \class TaskSetSourceMute
381  * \brief mutes a source
382  */
383 class TaskSetSourceMute: public Task {
384 Q_OBJECT
385 public:
386         TaskSetSourceMute(AudioManagerCore* core, source_t source);
387         virtual ~TaskSetSourceMute();
388         void executeTask(Queue* queue);
389 private:
390         source_t m_source; //!< the source to be muted
391 };
392
393 /**
394  * \class TaskSetSourceUnmute
395  * \brief unmutes a source
396  */
397 class TaskSetSourceUnmute: public Task {
398 Q_OBJECT
399 public:
400         TaskSetSourceUnmute(AudioManagerCore* core, source_t source);
401         virtual ~TaskSetSourceUnmute();
402         void executeTask(Queue* queue);
403 private:
404         source_t m_source; //!< the source to be unmuted
405 };
406
407 /**
408  * \class TaskEmitSignalConnect
409  * \brief emits the signal connect
410  */
411 class TaskEmitSignalConnect: public Task {
412 Q_OBJECT
413 public:
414         TaskEmitSignalConnect(AudioManagerCore* core);
415         virtual ~TaskEmitSignalConnect();
416         void executeTask(Queue* queue);
417 };
418
419 /**
420  * \class Queue
421  * \brief With the help of this class, all events that need to be processed are organized and queued
422  * \details tasks to be added have to be created with new, after the queue is done all tasks will be deleted and the memory freed.
423  * \todo error handling
424  * \todo sorting of tasks
425  * \todo add some function to attach parameters to the Queue so that organizing and handling is possible
426  *
427  * \fn Queue::Queue(AudioManagerCore* core,QString name="")
428  * \brief constructor
429  * \param name give the queue a name
430  * \param core pointer to AudioManagerCore
431  *
432  * \fn void Queue::run()
433  * \brief starts the queue
434  *
435  * \fn void Queue::addTask(Task* task)
436  * \brief add a task to the queue
437  * \param task pointer to the task
438  *
439  * \fn genError_t Queue::removeTask(Task* task)
440  * \brief removes a task
441  * \param task pointer to the task
442  * \return GEN_OK on success
443  *
444  * \fn  QList<Task*> Queue::getTaskList()
445  * \brief returns the actual task list
446  * \return list with pointers to the tasks
447  *
448  * \fn QString Queue::returnName()
449  * \brief returns the name of the Queue
450  * \return the name in QString format
451  *
452  * \fn void Queue::slot_nextTask()
453  * \brief is called when a task is finished and the next task can be called
454  */
455 class Queue: public QObject {
456 Q_OBJECT
457 public:
458         Queue(AudioManagerCore* core,QString name="");
459         virtual ~Queue();
460         void run();
461         void addTask(Task* task);
462         genError_t removeTask(Task* task);
463         QList<Task*> getTaskList();
464         QString returnName();
465
466 public slots:
467         void slot_nextTask();
468 private:
469         AudioManagerCore* m_core; //!< pointer to AudioManagerCore
470         int m_currentIndex; //!< the index of the list wich is currently worked on
471         QString m_name; //!< name of the Queue
472         QList<Task*> m_taskList; //!< the list with pointers to the tasks
473 };
474
475 /**
476  * \class AudioManagerCore
477  * \brief The central Managing Class of the AudioManager
478  *
479  * \fn genError_t AudioManagerCore::UserConnect(source_t source, sink_t sink)
480  * \brief does a user connect
481  *
482  * \fn genError_t AudioManagerCore::UserDisconnect(connection_t connID)
483  * \brief does a user disconnect
484  *
485  * \fn genError_t AudioManagerCore::UserSetVolume(sink_t sink, volume_t volume)
486  * \brief set the user volume (on sink level)
487  *
488  * \fn genError_t AudioManagerCore::connect(source_t source, sink_t sink, genHandle_t* handle = NULL)
489  * \brief connects sources and sinks
490  *
491  * \fn genError_t AudioManagerCore::disconnect(connection_t ID)
492  * \brief disconnects sources and sinks
493  *
494  * \fn genError_t AudioManagerCore::setVolume(sink_t sink, volume_t volume)
495  * \brief sets the volume to a sink
496  *
497  * \fn genError_t AudioManagerCore::interruptRequest(source_t interruptSource, sink_t sink, genInt_t* interruptID)
498  * \brief via this call, an interrupt is requested
499  *
500  * \fn genError_t AudioManagerCore::setSourceVolume (source_t source, volume_t volume)
501  * \brief sets the source volume
502  *
503  * \fn genError_t AudioManagerCore::setSourceMute (source_t source)
504  * \brief mutes a source
505  *
506  * \fn genError_t AudioManagerCore::setSourceUnMute (source_t source)
507  * \brief unmutes a source
508  *
509  * \fn genError_t AudioManagerCore::getRoute(const bool onlyfree, const source_t source, const sink_t sink, QList<genRoute_t>* ReturnList)
510  * \brief returns a route
511  *
512  * \fn connection_t AudioManagerCore::returnConnectionIDforSinkSource (sink_t sink, source_t source)
513  * \brief returns the connection ID for a sink source combination
514  *
515  * \fn source_t AudioManagerCore::returnSourceIDfromName(const QString name)
516  * \brief returns the source ID for a name
517  *
518  * \fn sink_t AudioManagerCore::returnSinkIDfromName(const QString name)
519  * \brief returns the sink ID for a given name
520  *
521  * \fn QList<ConnectionType> AudioManagerCore::getListConnections()
522  * \brief returns the list of connections
523  *
524  * \fn QList<SinkType> AudioManagerCore::getListSinks()
525  * \brief returns a list of all sinks
526  *
527  * \fn QList<SourceType> AudioManagerCore::getListSources()
528  * \brief returns a list of all sources
529  *
530  * \fn void AudioManagerCore::emitSignalConnect()
531  * \brief emits the signal connect
532  *
533  * \fn Router* AudioManagerCore::returnRouter()
534  * \brief returns the pointer to the router
535  *
536  * \fn DataBaseHandler* AudioManagerCore::returnDatabaseHandler()
537  * \brief returns the pointer to the database handler
538  *
539  * \fn RoutingReceiver* AudioManagerCore::returnReceiver()
540  * \brief returns the pointer to the receiver
541  *
542  * \fn DBusCommandInterface* AudioManagerCore::returnCommandInterface()
543  * \brief returns the pointer to the command interface
544  *
545  * \fn void AudioManagerCore::registerDatabasehandler(DataBaseHandler * handler)
546  * \brief registers the database handler @ the core
547  *
548  * \fn void AudioManagerCore::registerRouter(Router* router)
549  * \brief registers the router @ the core
550  *
551  * \fn void AudioManagerCore::registerBushandler(Bushandler* handler)
552  * \brief registers the bushandler @ the core
553  *
554  * \fn void AudioManagerCore::registerHookEngine(HookHandler* handler)
555  * \brief registers the hook engine @ the core
556  *
557  * \fn void AudioManagerCore::registerReceiver(RoutingReceiver* receiver)
558  * \brief registers the receiver @ the core
559  *
560  * \fn void AudioManagerCore::registerCommandInterface(DBusCommandInterface* command)
561  * \brief registers the command interface @ the core
562  *
563  * \fn void AudioManagerCore::addQueue(Queue* queue)
564  * \brief adds a queue
565  *
566  * \fn genError_t AudioManagerCore::removeQueue(Queue* queue)
567  * \brief removes a queue
568  *
569  *
570  */
571 class AudioManagerCore :public QObject {
572 Q_OBJECT
573 public:
574         AudioManagerCore();
575         virtual ~AudioManagerCore();
576
577         genError_t UserConnect(source_t source, sink_t sink);
578         genError_t UserDisconnect(connection_t connID);
579         genError_t UserSetVolume(sink_t sink, volume_t volume);
580         genError_t connect(source_t source, sink_t sink, genHandle_t* handle = NULL);
581         genError_t disconnect(connection_t ID);
582         genError_t setVolume(sink_t sink, volume_t volume);
583         genError_t interruptRequest(source_t interruptSource, sink_t sink, genInt_t* interruptID);
584         genError_t setSourceVolume (source_t source, volume_t volume);
585         genError_t setSourceMute (source_t source);
586         genError_t setSourceUnMute (source_t source);
587
588         genError_t getRoute(const bool onlyfree, const source_t source, const sink_t sink, QList<genRoute_t>* ReturnList);
589         connection_t returnConnectionIDforSinkSource (sink_t sink, source_t source);
590         source_t returnSourceIDfromName(const QString name);
591         sink_t returnSinkIDfromName(const QString name);
592
593         QList<ConnectionType> getListConnections();
594         QList<SinkType> getListSinks();
595         QList<SourceType> getListSources();
596
597         void emitSignalConnect();
598
599         Router* returnRouter();
600         DataBaseHandler* returnDatabaseHandler();
601         RoutingReceiver* returnReceiver();
602         DBusCommandInterface* returnCommandInterface();
603
604         void registerDatabasehandler(DataBaseHandler * handler);
605         void registerRouter(Router* router);
606         void registerBushandler(Bushandler* handler);
607         void registerHookEngine(HookHandler* handler);
608         void registerReceiver(RoutingReceiver* receiver);
609         void registerCommandInterface(DBusCommandInterface* command);
610
611         void addQueue(Queue* queue);
612         genError_t removeQueue(Queue* queue);
613
614 signals:
615         void signal_connectionChanged();
616         void signal_numberOfSinksChanged();
617         void signal_numberOfSourcesChanged();
618
619 private:
620         DataBaseHandler* m_databaseHandler; //!< pointer to the DataBasehandler Class
621         Router* m_router; //!< pointer to the Router Class
622         Bushandler* m_busHandler; //!< pointer to the Bushandler Class
623         HookHandler* m_hookHandler; //!< pointer to the HookHandler CLass
624         RoutingReceiver* m_receiver; //!< pointer to the Routing receiver Class
625         DBusCommandInterface* m_command; //!< pointer to the command Interface Class
626
627         QList<Queue*> m_queueList; //!< List of pointers to all running queues
628 };
629
630 #endif /* AUDIOMANAGERCORE_H_ */