* [GAM-55] some finetuning on bugfix.
[profile/ivi/genivi/genivi-audio-manager.git] / PluginRoutingInterfaceDbus / src / CAmRoutingSenderDbus.cpp
1 /**
2  *  Copyright (c) 2012 BMW
3  *  Copyright (c) copyright 2011-2012 Aricent® Group  and its licensors
4  *
5  *  \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
6  *  \author Sampreeth Ramavana
7  *
8  *  \copyright
9  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
10  *  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
11  *  subject to the following conditions:
12  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
14  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
15  *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16  *
17  *  For further information see http://www.genivi.org/.
18  */
19
20 #include "CAmRoutingSenderDbus.h"
21 #include <cassert>
22 #include <map>
23 #include "CAmDbusSend.h"
24 #include "shared/CAmDltWrapper.h"
25 #include "shared/CAmDbusWrapper.h"
26
27 namespace am
28 {
29 DLT_DECLARE_CONTEXT(routingDbus)
30
31 extern "C" IAmRoutingSend* PluginRoutingInterfaceDbusFactory()
32 {
33     return (new CAmRoutingSenderDbus());
34 }
35
36 extern "C" void destroyRoutingPluginInterfaceDbus(IAmRoutingSend* routingSendInterface)
37 {
38     delete routingSendInterface;
39 }
40
41 CAmRoutingSenderDbus::CAmRoutingSenderDbus() :
42         mpCAmDBusWrapper(), //
43         mpIAmRoutingReceive(), //
44         mpDBusConnection(), //
45         mCAmRoutingDBusMessageHandler(), //
46         mIAmRoutingReceiverShadowDbus(this)
47 {
48     log(&routingDbus, DLT_LOG_INFO, "RoutingSender constructed");
49 }
50
51 CAmRoutingSenderDbus::~CAmRoutingSenderDbus()
52 {
53     log(&routingDbus, DLT_LOG_INFO, "RoutingSender destructed");
54     CAmDltWrapper::instance()->unregisterContext(routingDbus);
55 }
56
57 am_Error_e CAmRoutingSenderDbus::startupInterface(IAmRoutingReceive* pIAmRoutingReceive)
58 {
59     log(&routingDbus, DLT_LOG_INFO, "startupInterface called");
60     mpIAmRoutingReceive = pIAmRoutingReceive;
61     mIAmRoutingReceiverShadowDbus.setRoutingReceiver(mpIAmRoutingReceive);
62     mpIAmRoutingReceive->getDBusConnectionWrapper(mpCAmDBusWrapper);
63     assert(mpCAmDBusWrapper!=NULL);
64     mpCAmDBusWrapper->getDBusConnection(mpDBusConnection);
65     assert(mpDBusConnection!=NULL);
66     mCAmRoutingDBusMessageHandler.setDBusConnection(mpDBusConnection);
67     return (E_OK);
68 }
69
70 void CAmRoutingSenderDbus::getInterfaceVersion(std::string & version) const
71 {
72     version = RoutingSendVersion;
73 }
74
75 void CAmRoutingSenderDbus::setRoutingReady(const uint16_t handle)
76 {
77     log(&routingDbus, DLT_LOG_INFO, "sending routingReady signal");
78     mCAmRoutingDBusMessageHandler.initSignal(std::string(ROUTING_NODE), "setRoutingReady");
79     mCAmRoutingDBusMessageHandler.sendMessage();
80     mIAmRoutingReceiverShadowDbus.gotReady(mMapDomains.size(),handle);
81 }
82
83 void CAmRoutingSenderDbus::setRoutingRundown(const uint16_t handle)
84 {
85     mCAmRoutingDBusMessageHandler.initSignal(std::string(ROUTING_NODE), "setRoutingRundown");
86     mCAmRoutingDBusMessageHandler.sendMessage();
87     mIAmRoutingReceiverShadowDbus.gotRundown(mMapDomains.size(),handle);
88 }
89
90 am_Error_e CAmRoutingSenderDbus::asyncAbort(const am_Handle_s handle)
91 {
92     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncAbort called");
93     mapHandles_t::iterator iter = mMapHandles.begin();
94     iter = mMapHandles.find(handle.handle);
95     if (iter != mMapHandles.end())
96     {
97         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncAbort");
98         send.append(handle.handle);
99         return (send.send());
100     }
101     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncAbort could not find interface");
102     return (E_UNKNOWN);
103
104 }
105
106 am_Error_e CAmRoutingSenderDbus::asyncConnect(const am_Handle_s handle, const am_connectionID_t connectionID, const am_sourceID_t sourceID, const am_sinkID_t sinkID, const am_ConnectionFormat_e connectionFormat)
107 {
108     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncConnect called");
109     mapSources_t::iterator iter = mMapSources.begin();
110     iter = mMapSources.find(sourceID);
111     if (iter != mMapSources.end())
112     {
113         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncConnect");
114         send.append(handle.handle);
115         send.append(connectionID);
116         send.append(sourceID);
117         send.append(sinkID);
118         send.append(static_cast<int16_t>(connectionFormat));
119         mMapConnections.insert(std::make_pair(connectionID, (iter->second)));
120         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
121         return (send.send());
122     }
123     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncConnect could not find interface");
124     return (E_UNKNOWN);
125 }
126
127 am_Error_e CAmRoutingSenderDbus::asyncDisconnect(const am_Handle_s handle, const am_connectionID_t connectionID)
128 {
129     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncDisconnect called");
130     mapConnections_t::iterator iter = mMapConnections.begin();
131     iter = mMapConnections.find(connectionID);
132     if (iter != mMapConnections.end())
133     {
134         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncDisconnect");
135         send.append(handle.handle);
136         send.append(connectionID);
137         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
138         return (send.send());
139     }
140     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncDisconnect could not find interface");
141     return (E_UNKNOWN);
142 }
143
144 am_Error_e CAmRoutingSenderDbus::asyncSetSinkVolume(const am_Handle_s handle, const am_sinkID_t sinkID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time)
145 {
146     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkVolume called");
147     mapSinks_t::iterator iter = mMapSinks.begin();
148     iter = mMapSinks.find(sinkID);
149     if (iter != mMapSinks.end())
150     {
151         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkVolume");
152         send.append(handle.handle);
153         send.append(sinkID);
154         send.append(volume);
155         send.append(static_cast<int16_t>(ramp));
156         send.append(time);
157         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
158         return (send.send());
159     }
160     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkVolume could not find interface");
161     return (E_UNKNOWN);
162 }
163
164 am_Error_e CAmRoutingSenderDbus::asyncSetSourceVolume(const am_Handle_s handle, const am_sourceID_t sourceID, const am_volume_t volume, const am_RampType_e ramp, const am_time_t time)
165 {
166     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceVolume called");
167     mapSources_t::iterator iter = mMapSources.begin();
168     iter = mMapSources.find(sourceID);
169     if (iter != mMapSources.end())
170     {
171         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceVolume");
172         send.append(handle.handle);
173         send.append(sourceID);
174         send.append(volume);
175         send.append(static_cast<int16_t>(ramp));
176         send.append(time);
177         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
178         return (send.send());
179     }
180     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceVolume could not find interface");
181     return (E_UNKNOWN);
182 }
183
184 am_Error_e CAmRoutingSenderDbus::asyncSetSourceState(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SourceState_e state)
185 {
186     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceState called");
187     mapSources_t::iterator iter = mMapSources.begin();
188     iter = mMapSources.find(sourceID);
189     if (iter != mMapSources.end())
190     {
191         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceState");
192         send.append(handle.handle);
193         send.append(sourceID);
194         send.append(static_cast<int16_t>(state));
195         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
196         return (send.send());
197     }
198     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceState could not find interface");
199     return (E_UNKNOWN);
200 }
201
202 am_Error_e CAmRoutingSenderDbus::asyncSetSinkSoundProperties(const am_Handle_s handle, const am_sinkID_t sinkID, const std::vector<am_SoundProperty_s>& listSoundProperties)
203 {
204     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkSoundProperties called");
205     mapSinks_t::iterator iter = mMapSinks.begin();
206     iter = mMapSinks.find(sinkID);
207     if (iter != mMapSinks.end())
208     {
209         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkSoundProperties");
210         send.append(handle.handle);
211         send.append(sinkID);
212         send.append(listSoundProperties);
213         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
214         return (send.send());
215     }
216     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkSoundProperties could not find interface");
217     return (E_UNKNOWN);
218 }
219
220 am_Error_e CAmRoutingSenderDbus::asyncSetSinkSoundProperty(const am_Handle_s handle, const am_sinkID_t sinkID, const am_SoundProperty_s& soundProperty)
221 {
222     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkSoundProperty called");
223     mapSinks_t::iterator iter = mMapSinks.begin();
224     iter = mMapSinks.find(sinkID);
225     if (iter != mMapSinks.end())
226     {
227         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkSoundProperty");
228         send.append(handle.handle);
229         send.append(sinkID);
230         send.append(soundProperty);
231         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
232         return (send.send());
233     }
234     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkSoundProperty could not find interface");
235     return (E_UNKNOWN);
236 }
237
238 am_Error_e CAmRoutingSenderDbus::asyncSetSourceSoundProperties(const am_Handle_s handle, const am_sourceID_t sourceID, const std::vector<am_SoundProperty_s>& listSoundProperties)
239 {
240     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceSoundProperties called");
241     mapSources_t::iterator iter = mMapSources.begin();
242     iter = mMapSources.find(sourceID);
243     if (iter != mMapSources.end())
244     {
245         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceSoundProperties");
246         send.append(handle.handle);
247         send.append(sourceID);
248         send.append(listSoundProperties);
249         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
250         return (send.send());
251     }
252     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceSoundProperties could not find interface");
253     return (E_UNKNOWN);
254 }
255
256 am_Error_e CAmRoutingSenderDbus::asyncSetSourceSoundProperty(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SoundProperty_s& soundProperty)
257 {
258     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceSoundProperty called");
259     mapSources_t::iterator iter = mMapSources.begin();
260     iter = mMapSources.find(sourceID);
261     if (iter != mMapSources.end())
262     {
263         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceSoundProperty");
264         send.append(handle.handle);
265         send.append(sourceID);
266         send.append(soundProperty);
267         mMapHandles.insert(std::make_pair(handle.handle, iter->second));
268         return (send.send());
269     }
270     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceSoundProperty could not find interface");
271     return (E_UNKNOWN);
272 }
273
274 am_Error_e CAmRoutingSenderDbus::asyncCrossFade(const am_Handle_s handle, const am_crossfaderID_t crossfaderID, const am_HotSink_e hotSink, const am_RampType_e rampType, const am_time_t time)
275 {
276     (void)handle;
277     (void)crossfaderID;
278     (void)hotSink;
279     (void)rampType;
280     (void)time;
281     //todo implement
282     return (E_NON_EXISTENT);
283 }
284
285 am_Error_e CAmRoutingSenderDbus::setDomainState(const am_domainID_t domainID, const am_DomainState_e domainState)
286 {
287     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::setDomainState called");
288     mapDomain_t::iterator iter = mMapDomains.begin();
289     iter = mMapDomains.find(domainID);
290     if (iter != mMapDomains.end())
291     {
292         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "setDomainState");
293         send.append(domainID);
294         send.append(static_cast<int16_t>(domainState));
295         return (send.send());
296     }
297     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::setDomainState could not find interface");
298     return (E_UNKNOWN);
299 }
300
301 am_Error_e CAmRoutingSenderDbus::returnBusName(std::string& BusName) const
302 {
303     BusName = "DbusRoutingPlugin";
304     return (E_OK);
305 }
306
307 void CAmRoutingSenderDbus::removeHandle(uint16_t handle)
308 {
309     mMapHandles.erase(handle);
310 }
311
312 void CAmRoutingSenderDbus::addDomainLookup(am_domainID_t domainID, rs_lookupData_s lookupData)
313 {
314     mMapDomains.insert(std::make_pair(domainID, lookupData));
315 }
316
317 void CAmRoutingSenderDbus::addSourceLookup(am_sourceID_t sourceID, am_domainID_t domainID)
318 {
319     mapDomain_t::iterator iter(mMapDomains.begin());
320     iter = mMapDomains.find(domainID);
321     if (iter != mMapDomains.end())
322     {
323         mMapSources.insert(std::make_pair(sourceID, iter->second));
324     }
325 }
326
327 void CAmRoutingSenderDbus::addSinkLookup(am_sinkID_t sinkID, am_domainID_t domainID)
328 {
329     mapDomain_t::iterator iter(mMapDomains.begin());
330     iter = mMapDomains.find(domainID);
331     if (iter != mMapDomains.end())
332     {
333         mMapSinks.insert(std::make_pair(sinkID, iter->second));
334     }
335 }
336
337 void CAmRoutingSenderDbus::removeDomainLookup(am_domainID_t domainID)
338 {
339     mMapHandles.erase(domainID);
340 }
341
342 void CAmRoutingSenderDbus::removeSourceLookup(am_sourceID_t sourceID)
343 {
344     mMapHandles.erase(sourceID);
345 }
346
347 void CAmRoutingSenderDbus::removeSinkLookup(am_sinkID_t sinkID)
348 {
349     mMapHandles.erase(sinkID);
350 }
351
352 }
353