* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / src / RoutingReceiver.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file RoutingReceiver.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section LicenseRoutingReceiver.h
12  *
13  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
14  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
15  *
16  * 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.
17  * 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.
18  * 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>.
19  * 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.
20  * 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.
21  * 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.
22  * 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.
23  *
24  */
25
26 #include "RoutingReceiver.h"
27 #include <SocketHandler.h>
28 #ifdef WITH_DBUS_WRAPPER
29 #include <dbus/DBusWrapper.h>
30 #endif
31 #include "DatabaseHandler.h"
32 #include "RoutingSender.h"
33 #include "ControlSender.h"
34 #include <assert.h>
35
36 using namespace am;
37
38 am::RoutingReceiver::RoutingReceiver(DatabaseHandler *iDatabaseHandler, RoutingSender *iRoutingSender, ControlSender *iControlSender, SocketHandler *iSocketHandler) :
39         mDatabaseHandler(iDatabaseHandler), //
40         mRoutingSender(iRoutingSender), //
41         mControlSender(iControlSender), //
42         mSocketHandler(iSocketHandler) //
43 {
44     assert(mDatabaseHandler!=NULL);
45     assert(mRoutingSender!=NULL);
46     assert(mControlSender!=NULL);
47     assert(mSocketHandler!=NULL);
48 }
49
50 am::RoutingReceiver::RoutingReceiver(DatabaseHandler *iDatabaseHandler, RoutingSender *iRoutingSender, ControlSender *iControlSender, DBusWrapper *iDBusWrapper) :
51         mDatabaseHandler(iDatabaseHandler), //
52         mRoutingSender(iRoutingSender), //
53         mControlSender(iControlSender), //
54         mDBusWrapper(iDBusWrapper) //
55 {
56     assert(mDatabaseHandler!=NULL);
57     assert(mRoutingSender!=NULL);
58     assert(mControlSender!=NULL);
59     assert(mDBusWrapper!=NULL);
60 }
61
62 am::RoutingReceiver::RoutingReceiver(DatabaseHandler *iDatabaseHandler, RoutingSender *iRoutingSender, ControlSender *iControlSender, SocketHandler *iSocketHandler, DBusWrapper *iDBusWrapper) :
63         mDatabaseHandler(iDatabaseHandler), //
64         mRoutingSender(iRoutingSender), //
65         mControlSender(iControlSender), //
66         mSocketHandler(iSocketHandler), //
67         mDBusWrapper(iDBusWrapper) //
68 {
69     assert(mDatabaseHandler!=NULL);
70     assert(mRoutingSender!=NULL);
71     assert(mControlSender!=NULL);
72     assert(mSocketHandler!=NULL);
73     assert(mDBusWrapper!=NULL);
74 }
75
76 RoutingReceiver::~RoutingReceiver()
77 {
78 }
79
80 void RoutingReceiver::ackConnect(const am_Handle_s handle, const am_connectionID_t connectionID, const am_Error_e error)
81 {
82     mRoutingSender->removeHandle(handle);
83     if (error == E_OK)
84     {
85         mDatabaseHandler->changeConnectionFinal(connectionID);
86     }
87     else
88     {
89         mDatabaseHandler->removeConnection(connectionID);
90     }
91     mControlSender->cbAckConnect(handle, error);
92 }
93
94 void RoutingReceiver::ackDisconnect(const am_Handle_s handle, const am_connectionID_t connectionID, const am_Error_e error)
95 {
96     mRoutingSender->removeHandle(handle);
97     if (error == E_OK)
98     {
99         mDatabaseHandler->removeConnection(connectionID);
100     }
101     mControlSender->cbAckDisconnect(handle, error);
102 }
103
104 void RoutingReceiver::ackSetSinkVolumeChange(const am_Handle_s handle, const am_volume_t volume, const am_Error_e error)
105 {
106     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
107     if (error == E_OK && handleData.sinkID != 0)
108     {
109         //todo: check if volume in handleData is same than volume. React to it.
110         mDatabaseHandler->changeSinkVolume(handleData.sinkID, volume);
111     }
112     mRoutingSender->removeHandle(handle);
113     mControlSender->cbAckSetSinkVolumeChange(handle, volume, error);
114 }
115
116 void RoutingReceiver::ackSetSourceVolumeChange(const am_Handle_s handle, const am_volume_t volume, const am_Error_e error)
117 {
118     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
119     if (error == E_OK && handleData.sourceID != 0)
120     {
121         //todo: check if volume in handleData is same than volume. React to it.
122         mDatabaseHandler->changeSourceVolume(handleData.sourceID, volume);
123     }
124     mRoutingSender->removeHandle(handle);
125     mControlSender->cbAckSetSourceVolumeChange(handle, volume, error);
126 }
127
128 void RoutingReceiver::ackSetSourceState(const am_Handle_s handle, const am_Error_e error)
129 {
130     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
131     if (error == E_OK && handleData.sourceID != 0)
132     {
133         //todo: check if volume in handleData is same than volume. React to it.
134         mDatabaseHandler->changeSourceState(handleData.sourceID, handleData.sourceState);
135     }
136     mRoutingSender->removeHandle(handle);
137     mControlSender->cbAckSetSourceState(handle, error);
138 }
139
140 void RoutingReceiver::ackSetSinkSoundProperty(const am_Handle_s handle, const am_Error_e error)
141 {
142     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
143     if (error == E_OK && handleData.sinkID != 0)
144     {
145         mDatabaseHandler->changeSinkSoundPropertyDB(handleData.soundPropery, handleData.sinkID);
146     }
147     mRoutingSender->removeHandle(handle);
148     mControlSender->cbAckSetSinkSoundProperty(handle, error);
149
150 }
151
152 void am::RoutingReceiver::ackSetSinkSoundProperties(const am_Handle_s handle, const am_Error_e error)
153 {
154     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
155     if (error == E_OK && handleData.sinkID != 0)
156     {
157         std::vector<am_SoundProperty_s>::const_iterator it = handleData.soundProperties->begin();
158         for (; it != handleData.soundProperties->end(); ++it)
159         {
160             mDatabaseHandler->changeSinkSoundPropertyDB(*it, handleData.sinkID);
161         }
162         delete handleData.soundProperties;
163     }
164     mRoutingSender->removeHandle(handle);
165     mControlSender->cbAckSetSinkSoundProperties(handle, error);
166 }
167
168 void RoutingReceiver::ackSetSourceSoundProperty(const am_Handle_s handle, const am_Error_e error)
169 {
170     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
171     if (error == E_OK && handleData.sourceID != 0)
172     {
173         mDatabaseHandler->changeSourceSoundPropertyDB(handleData.soundPropery, handleData.sourceID);
174     }
175     mRoutingSender->removeHandle(handle);
176     mControlSender->cbAckSetSourceSoundProperty(handle, error);
177 }
178
179 void am::RoutingReceiver::ackSetSourceSoundProperties(const am_Handle_s handle, const am_Error_e error)
180 {
181     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
182     if (error == E_OK && handleData.sourceID != 0)
183     {
184         std::vector<am_SoundProperty_s>::const_iterator it = handleData.soundProperties->begin();
185         for (; it != handleData.soundProperties->end(); ++it)
186         {
187             mDatabaseHandler->changeSourceSoundPropertyDB(*it, handleData.sourceID);
188         }
189         delete handleData.soundProperties;
190     }
191     mRoutingSender->removeHandle(handle);
192     mControlSender->cbAckSetSourceSoundProperties(handle, error);
193 }
194
195 void RoutingReceiver::ackCrossFading(const am_Handle_s handle, const am_HotSink_e hotSink, const am_Error_e error)
196 {
197     RoutingSender::am_handleData_c handleData = mRoutingSender->returnHandleData(handle);
198     if (error == E_OK && handleData.crossfaderID != 0)
199     {
200         //todo: check if volume in handleData is same than volume. React to it.
201         mDatabaseHandler->changeCrossFaderHotSink(handleData.crossfaderID, hotSink);
202     }
203     mRoutingSender->removeHandle(handle);
204     mControlSender->cbAckCrossFade(handle, hotSink, error);
205 }
206
207 void RoutingReceiver::ackSourceVolumeTick(const am_Handle_s handle, const am_sourceID_t sourceID, const am_volume_t volume)
208 {
209     mControlSender->hookSystemSourceVolumeTick(handle, sourceID, volume);
210 }
211
212 void RoutingReceiver::ackSinkVolumeTick(const am_Handle_s handle, const am_sinkID_t sinkID, const am_volume_t volume)
213 {
214     mControlSender->hookSystemSinkVolumeTick(handle, sinkID, volume);
215 }
216
217 am_Error_e RoutingReceiver::peekDomain(const std::string & name, am_domainID_t & domainID)
218 {
219     return mDatabaseHandler->peekDomain(name, domainID);
220
221 }
222
223 am_Error_e RoutingReceiver::registerDomain(const am_Domain_s & domainData, am_domainID_t & domainID)
224 {
225     return mControlSender->hookSystemRegisterDomain(domainData, domainID);
226 }
227
228 am_Error_e RoutingReceiver::deregisterDomain(const am_domainID_t domainID)
229 {
230     return mControlSender->hookSystemDeregisterDomain(domainID);
231 }
232
233 am_Error_e RoutingReceiver::registerGateway(const am_Gateway_s & gatewayData, am_gatewayID_t & gatewayID)
234 {
235     return mControlSender->hookSystemRegisterGateway(gatewayData, gatewayID);
236 }
237
238 am_Error_e RoutingReceiver::deregisterGateway(const am_gatewayID_t gatewayID)
239 {
240     return mControlSender->hookSystemDeregisterGateway(gatewayID);
241 }
242
243 am_Error_e RoutingReceiver::peekSink(const std::string& name, am_sinkID_t & sinkID)
244 {
245     return mDatabaseHandler->peekSink(name, sinkID);
246 }
247
248 am_Error_e RoutingReceiver::registerSink(const am_Sink_s & sinkData, am_sinkID_t & sinkID)
249 {
250     return mControlSender->hookSystemRegisterSink(sinkData, sinkID);
251 }
252
253 am_Error_e RoutingReceiver::deregisterSink(const am_sinkID_t sinkID)
254 {
255     return mControlSender->hookSystemDeregisterSink(sinkID);
256 }
257
258 am_Error_e RoutingReceiver::peekSource(const std::string & name, am_sourceID_t & sourceID)
259 {
260     return mDatabaseHandler->peekSource(name, sourceID);
261 }
262
263 am_Error_e RoutingReceiver::registerSource(const am_Source_s & sourceData, am_sourceID_t & sourceID)
264 {
265     return mControlSender->hookSystemRegisterSource(sourceData, sourceID);
266 }
267
268 am_Error_e RoutingReceiver::deregisterSource(const am_sourceID_t sourceID)
269 {
270     return mControlSender->hookSystemDeregisterSource(sourceID);
271 }
272
273 am_Error_e RoutingReceiver::registerCrossfader(const am_Crossfader_s & crossfaderData, am_crossfaderID_t & crossfaderID)
274 {
275     return mControlSender->hookSystemRegisterCrossfader(crossfaderData, crossfaderID);
276 }
277
278 am_Error_e RoutingReceiver::deregisterCrossfader(const am_crossfaderID_t crossfaderID)
279 {
280     return mControlSender->hookSystemDeregisterCrossfader(crossfaderID);
281 }
282
283 void RoutingReceiver::hookInterruptStatusChange(const am_sourceID_t sourceID, const am_InterruptState_e interruptState)
284 {
285     return mControlSender->hookSystemInterruptStateChange(sourceID, interruptState);
286 }
287
288 void RoutingReceiver::hookDomainRegistrationComplete(const am_domainID_t domainID)
289 {
290     mControlSender->hookSystemDomainRegistrationComplete(domainID);
291 }
292
293 void RoutingReceiver::hookSinkAvailablityStatusChange(const am_sinkID_t sinkID, const am_Availability_s & availability)
294 {
295     mControlSender->hookSystemSinkAvailablityStateChange(sinkID, availability);
296 }
297
298 void RoutingReceiver::hookSourceAvailablityStatusChange(const am_sourceID_t sourceID, const am_Availability_s & availability)
299 {
300     mControlSender->hookSystemSourceAvailablityStateChange(sourceID, availability);
301 }
302
303 void RoutingReceiver::hookDomainStateChange(const am_domainID_t domainID, const am_DomainState_e domainState)
304 {
305     mControlSender->hookSystemDomainStateChange(domainID, domainState);
306 }
307
308 void RoutingReceiver::hookTimingInformationChanged(const am_connectionID_t connectionID, const am_timeSync_t delay)
309 {
310     mDatabaseHandler->changeConnectionTimingInformation(connectionID, delay);
311 }
312
313 am_Error_e RoutingReceiver::sendChangedData(const std::vector<am_EarlyData_s> & earlyData)
314 {
315     mControlSender->hookSystemReceiveEarlyData(earlyData);
316     return E_OK;
317 //todo: change return type to void in EA model
318 }
319
320 am_Error_e RoutingReceiver::peekSinkClassID(const std::string name, const am_sinkClass_t& sinkClassID)
321 {
322 //todo: implement
323     return E_NOT_USED;
324 }
325
326 am_Error_e RoutingReceiver::peekSourceClassID(const std::string name, const am_sourceClass_t& sourceClassID)
327 {
328 //todo: implement
329     return E_NOT_USED;
330 }
331
332 am_Error_e RoutingReceiver::getDBusConnectionWrapper(DBusWrapper *& dbusConnectionWrapper) const
333 {
334 #ifdef WITH_DBUS_WRAPPER
335     dbusConnectionWrapper = mDBusWrapper;
336     return E_OK;
337 #else
338     return E_UNKNOWN;
339 #endif
340 }
341
342 am_Error_e RoutingReceiver::getSocketHandler(SocketHandler *& socketHandler) const
343 {
344 #ifdef WITH_SOCKETHANDLER_LOOP
345     socketHandler = mSocketHandler;
346     return E_OK;
347 #else
348     return E_UNKNOWN;
349 #endif
350 }
351
352 uint16_t RoutingReceiver::getInterfaceVersion() const
353 {
354     return RoutingReceiveVersion;
355 }
356