* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.git] / PluginCommandInterfaceDbus / src / DBusCommandSender.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger DbusPlugin
5  *
6  * \file CommandSender.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (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 Mueller  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  * THIS CODE HAS BEEN GENERATED BY ENTERPRISE ARCHITECT GENIVI MODEL. PLEASE CHANGE ONLY IN ENTERPRISE ARCHITECT AND GENERATE AGAIN
24  */
25
26 #include <command/CommandReceiveInterface.h>
27 #include <dbus/DBusWrapper.h>
28 #include "DBusCommandSender.h"
29 #include "DBusMessageHandler.h"
30 #include <dlt/dlt.h>
31 #include <algorithm>
32 #include <string>
33 #include <vector>
34 #include <assert.h>
35 #include <set>
36
37 using namespace am;
38
39 DLT_DECLARE_CONTEXT(DLT_CONTEXT)
40
41 /**
42  * factory for plugin loading
43  */
44 extern "C" CommandSendInterface* PluginCommandInterfaceDbusFactory()
45 {
46     return (new DbusCommandSender());
47 }
48
49 /**
50  * destroy instance of commandSendInterface
51  */
52 extern "C" void destroyRoutingPluginInterfaceDbus(CommandSendInterface* commandSendInterface)
53 {
54     delete commandSendInterface;
55 }
56
57 DbusCommandSender::DbusCommandSender() :
58         mDBUSMessageHandler(), //
59         mCommandReceiverShadow(), //
60         mDBusWrapper(NULL), //
61         mCommandReceiveInterface(NULL)
62 {
63     DLT_REGISTER_CONTEXT(DLT_CONTEXT, "DBP", "DBus Plugin");
64     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("DbusCommandSender constructed"));
65 }
66
67 DbusCommandSender::~DbusCommandSender()
68 {
69     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("DbusCommandSender destructed"));
70     DLT_UNREGISTER_CONTEXT(DLT_CONTEXT);
71 }
72
73 am_Error_e DbusCommandSender::startupInterface(CommandReceiveInterface* commandreceiveinterface)
74 {
75     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("startupInterface called"));
76
77     mCommandReceiveInterface = commandreceiveinterface;
78     mCommandReceiverShadow.setCommandReceiver(mCommandReceiveInterface);
79     mCommandReceiveInterface->getDBusConnectionWrapper(mDBusWrapper);
80     assert(mDBusWrapper!=NULL);
81     return (E_OK);
82 }
83
84 am_Error_e DbusCommandSender::stopInterface()
85 {
86     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("stopInterface called"));
87     /**
88      * todo: finish DbusCommandSender::stopInterface(), what needs to be done?
89      */
90     return (E_OK);
91 }
92
93 am_Error_e DbusCommandSender::cbCommunicationReady()
94 {
95     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbCommunicationReady called"));
96     /**
97      * todo: implement DbusCommandSender::cbCommunicationReady()
98      */
99     return E_NOT_USED;
100 }
101
102 am_Error_e DbusCommandSender::cbCommunicationRundown()
103 {
104     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbCommunicationRundown called"));
105     /**
106      * todo: implement DbusCommandSender::cbCommunicationRundown()
107      */
108     return E_NOT_USED;
109 }
110
111 void DbusCommandSender::cbNumberOfMainConnectionsChanged()
112 {
113     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbNumberOfMainConnectionsChanged called"));
114
115     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("NumberOfMainConnectionsChanged"));
116     mDBUSMessageHandler.sendMessage();
117 }
118
119 void DbusCommandSender::cbNumberOfSinksChanged()
120 {
121     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbNumberOfSinksChanged called"));
122
123     std::vector<am_SinkType_s> newListSinks;
124     std::vector<am_SinkType_s> diffList;
125     mCommandReceiveInterface->getListMainSinks(newListSinks);
126     std::sort(newListSinks.begin(), newListSinks.end(), sortBySinkID());
127     std::set_symmetric_difference(newListSinks.begin(), newListSinks.end(), mlistSinks.begin(), mlistSinks.end(), std::back_inserter(diffList), sortBySinkID());
128     assert(diffList.size()==1);
129     if (newListSinks.size() > mlistSinks.size())
130     {
131         mDBUSMessageHandler.initSignal(std::string(MY_NODE), "SinkAdded");
132         mDBUSMessageHandler.append(diffList[0]);
133
134         DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("send signal SinkAdded"));
135     }
136     else
137     {
138         mDBUSMessageHandler.initSignal(std::string(MY_NODE), "SinkRemoved");
139         mDBUSMessageHandler.append(diffList.begin()->sinkID);
140
141         DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("send signal SinkRemoved"));
142     }
143
144     mDBUSMessageHandler.sendMessage();
145     mlistSinks = newListSinks;
146 }
147
148 void DbusCommandSender::cbNumberOfSourcesChanged()
149 {
150     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbNumberOfSourcesChanged called"));
151
152     std::vector<am_SourceType_s> newlistSources;
153     std::vector<am_SourceType_s> diffList;
154     mCommandReceiveInterface->getListMainSources(newlistSources);
155     std::sort(newlistSources.begin(), newlistSources.end(), sortBySourceID());
156     std::set_symmetric_difference(newlistSources.begin(), newlistSources.end(), mlistSources.begin(), mlistSources.end(), std::back_inserter(diffList), sortBySourceID());
157     assert(diffList.size()==1);
158     if (newlistSources.size() > mlistSources.size())
159     {
160         mDBUSMessageHandler.initSignal(std::string(MY_NODE), "SourceAdded");
161         mDBUSMessageHandler.append(diffList[0]);
162
163         DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("send signal SourceAdded"));
164     }
165     else
166     {
167         mDBUSMessageHandler.initSignal(std::string(MY_NODE), "SourceRemoved");
168         mDBUSMessageHandler.append((dbus_uint16_t) diffList.begin()->sourceID);
169
170         DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("send signal SourceRemoved"));
171     }
172
173     mDBUSMessageHandler.sendMessage();
174     mlistSources = newlistSources;
175 }
176
177 void DbusCommandSender::cbNumberOfSinkClassesChanged()
178 {
179     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbNumberOfSinkClassesChanged called"));
180
181     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("NumberOfSinkClassesChanged"));
182     mDBUSMessageHandler.sendMessage();
183 }
184
185 void DbusCommandSender::cbNumberOfSourceClassesChanged()
186 {
187     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbNumberOfSourceClassesChanged called"));
188
189     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("NumberOfSourceClassesChanged"));
190     mDBUSMessageHandler.sendMessage();
191 }
192
193 void DbusCommandSender::cbMainConnectionStateChanged(const am_mainConnectionID_t connectionID, const am_ConnectionState_e connectionState)
194 {
195     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbMainConnectionStateChanged called, connectionID"), DLT_INT16(connectionID), DLT_STRING("connectionState"), DLT_INT16(connectionState));
196
197     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("MainConnectionStateChanged"));
198     mDBUSMessageHandler.append((dbus_uint16_t) connectionID);
199     mDBUSMessageHandler.append((dbus_int16_t) connectionState);
200     mDBUSMessageHandler.sendMessage();
201 }
202
203 void DbusCommandSender::cbMainSinkSoundPropertyChanged(const am_sinkID_t sinkID, const am_MainSoundProperty_s SoundProperty)
204 {
205     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbMainSinkSoundPropertyChanged called, sinkID"), DLT_INT16(sinkID), DLT_STRING("SoundProperty.type"), DLT_INT16(SoundProperty.type), DLT_STRING("SoundProperty.value"), DLT_INT16(SoundProperty.value));
206
207     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("MainSinkSoundPropertyChanged"));
208     mDBUSMessageHandler.append((dbus_uint16_t) sinkID);
209     mDBUSMessageHandler.append(SoundProperty);
210     mDBUSMessageHandler.sendMessage();
211 }
212
213 void DbusCommandSender::cbMainSourceSoundPropertyChanged(const am_sourceID_t sourceID, const am_MainSoundProperty_s & SoundProperty)
214 {
215     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbMainSourceSoundPropertyChanged called, sourceID"), DLT_INT16(sourceID), DLT_STRING("SoundProperty.type"), DLT_INT16(SoundProperty.type), DLT_STRING("SoundProperty.value"), DLT_INT16(SoundProperty.value));
216
217     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("MainSourceSoundPropertyChanged"));
218     mDBUSMessageHandler.append((dbus_uint16_t) sourceID);
219     mDBUSMessageHandler.append(SoundProperty);
220     mDBUSMessageHandler.sendMessage();
221 }
222
223 void DbusCommandSender::cbSinkAvailabilityChanged(const am_sinkID_t sinkID, const am_Availability_s & availability)
224 {
225     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbSinkAvailabilityChanged called, sinkID"), DLT_INT16(sinkID), DLT_STRING("availability.availability"), DLT_INT16(availability.availability), DLT_STRING("SoundProperty.reason"), DLT_INT16(availability.availabilityReason));
226
227     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("SinkAvailabilityChanged"));
228     mDBUSMessageHandler.append((dbus_uint16_t) sinkID);
229     mDBUSMessageHandler.append(availability);
230     mDBUSMessageHandler.sendMessage();
231 }
232
233 void DbusCommandSender::cbSourceAvailabilityChanged(const am_sourceID_t sourceID, const am_Availability_s & availability)
234 {
235     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbSourceAvailabilityChanged called, sourceID"), DLT_INT16(sourceID), DLT_STRING("availability.availability"), DLT_INT16(availability.availability), DLT_STRING("SoundProperty.reason"), DLT_INT16(availability.availabilityReason));
236
237     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("SourceAvailabilityChanged"));
238     mDBUSMessageHandler.append((dbus_uint16_t) sourceID);
239     mDBUSMessageHandler.append(availability);
240     mDBUSMessageHandler.sendMessage();
241 }
242
243 void DbusCommandSender::cbVolumeChanged(const am_sinkID_t sinkID, const am_mainVolume_t volume)
244 {
245     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbVolumeChanged called, sinkID"), DLT_INT16(sinkID), DLT_STRING("volume"), DLT_INT16(volume));
246
247     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("VolumeChanged"));
248     mDBUSMessageHandler.append((dbus_uint16_t) sinkID);
249     mDBUSMessageHandler.append((dbus_int16_t) volume);
250     mDBUSMessageHandler.sendMessage();
251 }
252
253 void DbusCommandSender::cbSinkMuteStateChanged(const am_sinkID_t sinkID, const am_MuteState_e muteState)
254 {
255     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbSinkMuteStateChanged called, sinkID"), DLT_INT16(sinkID), DLT_STRING("muteState"), DLT_INT16(muteState));
256
257     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("SinkMuteStateChanged"));
258     mDBUSMessageHandler.append((dbus_uint16_t) sinkID);
259     mDBUSMessageHandler.append((dbus_int16_t) muteState);
260     mDBUSMessageHandler.sendMessage();
261 }
262
263 void DbusCommandSender::cbSystemPropertyChanged(const am_SystemProperty_s & SystemProperty)
264 {
265     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbSystemPropertyChanged called, SystemProperty.type"), DLT_INT16(SystemProperty.type), DLT_STRING("SystemProperty.value"), DLT_INT16(SystemProperty.value));
266
267     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("SystemPropertyChanged"));
268     mDBUSMessageHandler.append(SystemProperty);
269     mDBUSMessageHandler.sendMessage();
270 }
271
272 void am::DbusCommandSender::cbTimingInformationChanged(const am_timeSync_t time, const am_mainConnectionID_t mainConnectionID)
273 {
274     DLT_LOG(DLT_CONTEXT, DLT_LOG_INFO, DLT_STRING("cbTimingInformationChanged called, mainConnectionID"), DLT_INT16(mainConnectionID), DLT_STRING("time"), DLT_INT16(time));
275
276     mDBUSMessageHandler.initSignal(std::string(MY_NODE), std::string("TimingInformationChanged"));
277     mDBUSMessageHandler.append((dbus_uint16_t) mainConnectionID);
278     mDBUSMessageHandler.append((dbus_int16_t) time);
279     mDBUSMessageHandler.sendMessage();
280 }
281
282 uint16_t DbusCommandSender::getInterfaceVersion() const
283 {
284     return (CommandSendVersion);
285 }
286