* Applied routing_send_async_calls.patch
[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  *  \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
8  *
9  *  \copyright
10  *  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,
11  *  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,
12  *  subject to the following conditions:
13  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *  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.
15  *  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
16  *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17  *
18  *  For further information see http://www.genivi.org/.
19  */
20
21 #include "CAmRoutingSenderDbus.h"
22 #include <cassert>
23 #include <map>
24 #include "CAmDbusSend.h"
25 #include "shared/CAmDltWrapper.h"
26 #include "shared/CAmDbusWrapper.h"
27
28 namespace am
29 {
30 DLT_DECLARE_CONTEXT(routingDbus)
31
32 extern "C" IAmRoutingSend* PluginRoutingInterfaceDbusFactory()
33 {
34     CAmDltWrapper::instance()->registerContext(routingDbus, "DRS", "DBus Plugin");
35     return (new CAmRoutingSenderDbus());
36 }
37
38 extern "C" void destroyRoutingPluginInterfaceDbus(IAmRoutingSend* routingSendInterface)
39 {
40     delete routingSendInterface;
41 }
42
43 CAmRoutingSenderDbus::CAmRoutingSenderDbus() :
44         mpCAmDBusWrapper(), //
45         mpIAmRoutingReceive(), //
46         mpDBusConnection(), //
47         mCAmRoutingDBusMessageHandler(), //
48         mIAmRoutingReceiverShadowDbus(this)
49 {
50     log(&routingDbus, DLT_LOG_INFO, "RoutingSender constructed");
51 }
52
53 CAmRoutingSenderDbus::~CAmRoutingSenderDbus()
54 {
55     log(&routingDbus, DLT_LOG_INFO, "RoutingSender destructed");
56     CAmDltWrapper::instance()->unregisterContext(routingDbus);
57 }
58
59 am_Error_e CAmRoutingSenderDbus::startupInterface(IAmRoutingReceive* pIAmRoutingReceive)
60 {
61     log(&routingDbus, DLT_LOG_INFO, "startupInterface called");
62     mpIAmRoutingReceive = pIAmRoutingReceive;
63     mIAmRoutingReceiverShadowDbus.setRoutingReceiver(mpIAmRoutingReceive);
64     mpIAmRoutingReceive->getDBusConnectionWrapper(mpCAmDBusWrapper);
65     assert(mpCAmDBusWrapper!=NULL);
66     mpCAmDBusWrapper->getDBusConnection(mpDBusConnection);
67     assert(mpDBusConnection!=NULL);
68     mCAmRoutingDBusMessageHandler.setDBusConnection(mpDBusConnection);
69     return (E_OK);
70 }
71
72 void CAmRoutingSenderDbus::getInterfaceVersion(std::string & version) const
73 {
74     version = RoutingSendVersion;
75 }
76
77 void CAmRoutingSenderDbus::setRoutingReady(const uint16_t handle)
78 {
79     log(&routingDbus, DLT_LOG_INFO, "sending routingReady signal");
80     mCAmRoutingDBusMessageHandler.initSignal(std::string(ROUTING_NODE), "setRoutingReady");
81     mCAmRoutingDBusMessageHandler.sendMessage();
82     mIAmRoutingReceiverShadowDbus.gotReady(mMapDomains.size(),handle);
83 }
84
85 void CAmRoutingSenderDbus::setRoutingRundown(const uint16_t handle)
86 {
87     mCAmRoutingDBusMessageHandler.initSignal(std::string(ROUTING_NODE), "setRoutingRundown");
88     mCAmRoutingDBusMessageHandler.sendMessage();
89     mIAmRoutingReceiverShadowDbus.gotRundown(mMapDomains.size(),handle);
90 }
91
92 am_Error_e CAmRoutingSenderDbus::asyncAbort(const am_Handle_s handle)
93 {
94     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncAbort called");
95     mapHandles_t::iterator iter = mMapHandles.begin();
96     iter = mMapHandles.find(handle.handle);
97     if (iter != mMapHandles.end())
98     {
99         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncAbort");
100         send.append(handle.handle);
101         return (send.send());
102     }
103     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncAbort could not find interface");
104     return (E_UNKNOWN);
105
106 }
107
108 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)
109 {
110     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncConnect called");
111     mapSources_t::iterator iter = mMapSources.begin();
112     iter = mMapSources.find(sourceID);
113     if (iter != mMapSources.end())
114     {
115         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncConnect");
116         send.append(handle.handle);
117         send.append(connectionID);
118         send.append(sourceID);
119         send.append(sinkID);
120         send.append(static_cast<int32_t>(connectionFormat));
121         mMapConnections.insert(std::make_pair(connectionID, (iter->second)));
122         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
123         return (send.sendAsync());
124     }
125     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncConnect could not find interface");
126     return (E_UNKNOWN);
127 }
128
129 am_Error_e CAmRoutingSenderDbus::asyncDisconnect(const am_Handle_s handle, const am_connectionID_t connectionID)
130 {
131     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncDisconnect called");
132     mapConnections_t::iterator iter = mMapConnections.begin();
133     iter = mMapConnections.find(connectionID);
134     if (iter != mMapConnections.end())
135     {
136         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncDisconnect");
137         send.append(handle.handle);
138         send.append(connectionID);
139         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
140         return (send.sendAsync());
141     }
142     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncDisconnect could not find interface");
143     return (E_UNKNOWN);
144 }
145
146 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)
147 {
148     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkVolume called");
149     mapSinks_t::iterator iter = mMapSinks.begin();
150     iter = mMapSinks.find(sinkID);
151     if (iter != mMapSinks.end())
152     {
153         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkVolume");
154         send.append(handle.handle);
155         send.append(sinkID);
156         send.append(volume);
157         send.append(static_cast<int16_t>(ramp));
158         send.append(time);
159         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
160         return (send.sendAsync());
161     }
162     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkVolume could not find interface");
163     return (E_UNKNOWN);
164 }
165
166 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)
167 {
168     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceVolume called");
169     mapSources_t::iterator iter = mMapSources.begin();
170     iter = mMapSources.find(sourceID);
171     if (iter != mMapSources.end())
172     {
173         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceVolume");
174         send.append(handle.handle);
175         send.append(sourceID);
176         send.append(volume);
177         send.append(static_cast<int16_t>(ramp));
178         send.append(time);
179         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
180         return (send.sendAsync());
181     }
182     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceVolume could not find interface");
183     return (E_UNKNOWN);
184 }
185
186 am_Error_e CAmRoutingSenderDbus::asyncSetSourceState(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SourceState_e state)
187 {
188     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceState called");
189     mapSources_t::iterator iter = mMapSources.begin();
190     iter = mMapSources.find(sourceID);
191     if (iter != mMapSources.end())
192     {
193         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceState");
194         send.append(handle.handle);
195         send.append(sourceID);
196         send.append(static_cast<int32_t>(state));
197         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
198         return (send.sendAsync());
199     }
200     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceState could not find interface");
201     return (E_UNKNOWN);
202 }
203
204 am_Error_e CAmRoutingSenderDbus::asyncSetSinkSoundProperties(const am_Handle_s handle, const am_sinkID_t sinkID, const std::vector<am_SoundProperty_s>& listSoundProperties)
205 {
206     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkSoundProperties called");
207     mapSinks_t::iterator iter = mMapSinks.begin();
208     iter = mMapSinks.find(sinkID);
209     if (iter != mMapSinks.end())
210     {
211         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkSoundProperties");
212         send.append(handle.handle);
213         send.append(sinkID);
214         send.append(listSoundProperties);
215         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
216         return (send.sendAsync());
217     }
218     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkSoundProperties could not find interface");
219     return (E_UNKNOWN);
220 }
221
222 am_Error_e CAmRoutingSenderDbus::asyncSetSinkSoundProperty(const am_Handle_s handle, const am_sinkID_t sinkID, const am_SoundProperty_s& soundProperty)
223 {
224     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSinkSoundProperty called");
225     mapSinks_t::iterator iter = mMapSinks.begin();
226     iter = mMapSinks.find(sinkID);
227     if (iter != mMapSinks.end())
228     {
229         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSinkSoundProperty");
230         send.append(handle.handle);
231         send.append(sinkID);
232         send.append(soundProperty);
233         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
234         return (send.sendAsync());
235     }
236     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSinkSoundProperty could not find interface");
237     return (E_UNKNOWN);
238 }
239
240 am_Error_e CAmRoutingSenderDbus::asyncSetSourceSoundProperties(const am_Handle_s handle, const am_sourceID_t sourceID, const std::vector<am_SoundProperty_s>& listSoundProperties)
241 {
242     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceSoundProperties called");
243     mapSources_t::iterator iter = mMapSources.begin();
244     iter = mMapSources.find(sourceID);
245     if (iter != mMapSources.end())
246     {
247         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceSoundProperties");
248         send.append(handle.handle);
249         send.append(sourceID);
250         send.append(listSoundProperties);
251         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
252         return (send.sendAsync());
253     }
254     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceSoundProperties could not find interface");
255     return (E_UNKNOWN);
256 }
257
258 am_Error_e CAmRoutingSenderDbus::asyncSetSourceSoundProperty(const am_Handle_s handle, const am_sourceID_t sourceID, const am_SoundProperty_s& soundProperty)
259 {
260     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::asyncSetSourceSoundProperty called");
261     mapSources_t::iterator iter = mMapSources.begin();
262     iter = mMapSources.find(sourceID);
263     if (iter != mMapSources.end())
264     {
265         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "asyncSetSourceSoundProperty");
266         send.append(handle.handle);
267         send.append(sourceID);
268         send.append(soundProperty);
269         mMapHandles.insert(std::make_pair(+handle.handle, iter->second));
270         return (send.sendAsync());
271     }
272     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::asyncSetSourceSoundProperty could not find interface");
273     return (E_UNKNOWN);
274 }
275
276 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)
277 {
278     (void)handle;
279     (void)crossfaderID;
280     (void)hotSink;
281     (void)rampType;
282     (void)time;
283     //todo implement
284     return (E_NON_EXISTENT);
285 }
286
287 am_Error_e CAmRoutingSenderDbus::setDomainState(const am_domainID_t domainID, const am_DomainState_e domainState)
288 {
289     log(&routingDbus, DLT_LOG_INFO, "CAmRoutingSenderDbus::setDomainState called");
290     mapDomain_t::iterator iter = mMapDomains.begin();
291     iter = mMapDomains.find(domainID);
292     if (iter != mMapDomains.end())
293     {
294         CAmRoutingDbusSend send(mpDBusConnection, iter->second.busname, iter->second.path, iter->second.interface, "setDomainState");
295         send.append(domainID);
296         send.append(static_cast<uint16_t>(domainState));
297         return (send.send());
298     }
299     log(&routingDbus, DLT_LOG_ERROR, "CAmRoutingSenderDbus::setDomainState could not find interface");
300     return (E_UNKNOWN);
301 }
302
303 am_Error_e CAmRoutingSenderDbus::returnBusName(std::string& BusName) const
304 {
305     BusName = "DbusRoutingPlugin";
306     return (E_OK);
307 }
308
309 void CAmRoutingSenderDbus::removeHandle(uint16_t handle)
310 {
311     mMapHandles.erase(handle);
312 }
313
314 void CAmRoutingSenderDbus::addDomainLookup(am_domainID_t domainID, rs_lookupData_s lookupData)
315 {
316     mMapDomains.insert(std::make_pair(domainID, lookupData));
317 }
318
319 void CAmRoutingSenderDbus::addSourceLookup(am_sourceID_t sourceID, am_domainID_t domainID)
320 {
321     mapDomain_t::iterator iter(mMapDomains.begin());
322     iter = mMapDomains.find(domainID);
323     if (iter != mMapDomains.end())
324     {
325         mMapSources.insert(std::make_pair(sourceID, iter->second));
326     }
327 }
328
329 void CAmRoutingSenderDbus::addSinkLookup(am_sinkID_t sinkID, am_domainID_t domainID)
330 {
331     mapDomain_t::iterator iter(mMapDomains.begin());
332     iter = mMapDomains.find(domainID);
333     if (iter != mMapDomains.end())
334     {
335         mMapSinks.insert(std::make_pair(sinkID, iter->second));
336     }
337 }
338
339 template <typename TKey> void  CAmRoutingSenderDbus::removeEntriesForValue(const rs_lookupData_s & value, std::map<TKey,rs_lookupData_s> & map)
340 {
341         typename std::map<TKey,rs_lookupData_s>::iterator it = map.begin();
342         while ( it != map.end() )
343         {
344                 if (it->second.busname == value.busname &&
345                         it->second.interface == value.interface &&
346                         it->second.path == value.path)
347                 {
348                         typename std::map<TKey,rs_lookupData_s>::iterator it_tmp = it;
349                         it++;
350                         map.erase(it_tmp);
351                 }
352                 else
353                         ++it;
354         }
355 }
356
357 void CAmRoutingSenderDbus::removeDomainLookup(am_domainID_t domainID)
358 {
359     mapDomain_t::iterator iter(mMapDomains.begin());
360     iter = mMapDomains.find(domainID);
361     if (iter != mMapDomains.end())
362     {
363         CAmRoutingSenderDbus::removeEntriesForValue(iter->second, mMapSources);
364         CAmRoutingSenderDbus::removeEntriesForValue(iter->second, mMapSinks);
365         CAmRoutingSenderDbus::removeEntriesForValue(iter->second, mMapHandles);
366         CAmRoutingSenderDbus::removeEntriesForValue(iter->second, mMapConnections);
367                 mMapDomains.erase(domainID);
368     }
369 }
370
371 void CAmRoutingSenderDbus::removeSourceLookup(am_sourceID_t sourceID)
372 {
373         mMapSources.erase(sourceID);
374 }
375
376 void CAmRoutingSenderDbus::removeSinkLookup(am_sinkID_t sinkID)
377 {
378     mMapSinks.erase(sinkID);
379 }
380
381 am_Error_e CAmRoutingSenderDbus::asyncSetVolumes(const am_Handle_s handle, const std::vector<am_Volumes_s>& listVolumes)
382 {
383     (void) handle;
384     (void) listVolumes;
385     //todo: implement asyncSetVolumes;
386     return (E_NOT_USED);
387 }
388
389 am_Error_e CAmRoutingSenderDbus::asyncSetSinkNotificationConfiguration(const am_Handle_s handle, const am_sinkID_t sinkID, const am_NotificationConfiguration_s& notificationConfiguration)
390 {
391     (void) handle;
392     (void) sinkID;
393     (void) notificationConfiguration;
394     //todo: implement asyncSetSinkNotificationConfiguration;
395     return (E_NOT_USED);
396 }
397
398 am_Error_e CAmRoutingSenderDbus::asyncSetSourceNotificationConfiguration(const am_Handle_s handle, const am_sourceID_t sourceID, const am_NotificationConfiguration_s& notificationConfiguration)
399 {
400     (void) handle;
401     (void) sourceID;
402     (void) notificationConfiguration;
403     //todo: implement asyncSetSourceNotificationConfiguration;
404     return (E_NOT_USED);
405 }
406
407 }
408