* fix compile error in DLTWrapper.cpp
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / CommandSender.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
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  */
24
25 #include "CommandSender.h"
26 #include <dirent.h>
27 #include <sstream>
28 #include <string>
29 #include "CommandReceiver.h"
30 #include "PluginTemplate.h"
31 #include "DLTWrapper.h"
32
33 using namespace am;
34
35 #define REQUIRED_INTERFACE_VERSION_MAJOR 1
36 #define REQUIRED_INTERFACE_VERSION_MINOR 0
37
38 //!< macro to call all interfaces
39 #define CALL_ALL_INTERFACES(...)                                                                                                                 \
40                 std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();     \
41                 std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();    \
42                 for (; iter<iterEnd;++iter)                                                                                                              \
43                 {                                                                                                                                                                \
44                         (*iter)->__VA_ARGS__;                                                                                                            \
45                 }
46
47 CommandSender::CommandSender(const std::vector<std::string>& listOfPluginDirectories) :
48         mListInterfaces(), //
49         mListLibraryHandles(), //
50         mListLibraryNames(), //
51         mCommandReceiver()
52 {
53     std::vector<std::string> sharedLibraryNameList;
54     std::vector<std::string>::const_iterator dirIter = listOfPluginDirectories.begin();
55     std::vector<std::string>::const_iterator dirIterEnd = listOfPluginDirectories.end();
56
57     // search communicator plugins in configured directories
58     for (; dirIter < dirIterEnd; ++dirIter)
59     {
60         const char* directoryName = dirIter->c_str();
61         logInfo("Searching for CommandPlugins in", *dirIter);
62         DIR *directory = opendir(directoryName);
63
64         if (!directory)
65         {
66             logError("Error opening directory ", *dirIter);
67             continue;
68         }
69
70         // iterate content of directory
71         struct dirent *itemInDirectory = 0;
72         while ((itemInDirectory = readdir(directory)))
73         {
74             unsigned char entryType = itemInDirectory->d_type;
75             std::string entryName = itemInDirectory->d_name;
76
77             bool regularFile = (entryType == DT_REG || entryType == DT_LNK);
78             bool sharedLibExtension = ("so" == entryName.substr(entryName.find_last_of(".") + 1));
79
80             if (regularFile && sharedLibExtension)
81             {
82                 std::string name(directoryName);
83                 sharedLibraryNameList.push_back(name + "/" + entryName);
84             }
85         }
86         closedir(directory);
87     }
88
89     // iterate all communicator plugins and start them
90     std::vector<std::string>::iterator iter = sharedLibraryNameList.begin();
91     std::vector<std::string>::iterator iterEnd = sharedLibraryNameList.end();
92
93     for (; iter < iterEnd; ++iter)
94     {
95         logInfo("Loading CommandSender plugin", *iter);
96         CommandSendInterface* (*createFunc)();
97         void* tempLibHandle = NULL;
98         createFunc = getCreateFunction<CommandSendInterface*()>(*iter, tempLibHandle);
99
100         if (!createFunc)
101         {
102             logInfo("Entry point of CommandPlugin not found", *iter);
103             continue;
104         }
105
106         CommandSendInterface* commander = createFunc();
107
108         if (!commander)
109         {
110             logInfo("CommandPlugin initialization failed. Entry Function not callable");
111             continue;
112         }
113
114         //check libversion
115         std::string version;
116         commander->getInterfaceVersion(version);
117         uint16_t minorVersion, majorVersion;
118         std::istringstream(version.substr(0, 1)) >> majorVersion;
119         std::istringstream(version.substr(2, 1)) >> minorVersion;
120
121         if (majorVersion < REQUIRED_INTERFACE_VERSION_MAJOR || ((majorVersion == REQUIRED_INTERFACE_VERSION_MAJOR) && (minorVersion > REQUIRED_INTERFACE_VERSION_MINOR)))
122         {
123             logInfo("CommandInterface initialization failed. Version of Interface to old");
124             continue;
125         }
126
127         mListInterfaces.push_back(commander);
128         mListLibraryHandles.push_back(tempLibHandle);
129         mListLibraryNames.push_back(iter->c_str());
130     }
131 }
132
133 CommandSender::~CommandSender()
134 {
135     unloadLibraries();
136 }
137
138 am_Error_e CommandSender::startupInterfaces(CommandReceiver *iCommandReceiver)
139 {
140     mCommandReceiver = iCommandReceiver;
141     am_Error_e returnError = E_OK;
142
143     std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();
144     std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();
145     for (; iter < iterEnd; ++iter)
146     {
147         am_Error_e error = (*iter)->startupInterface(iCommandReceiver);
148         if (error != E_OK)
149         {
150             returnError = error;
151         }
152     }
153     return returnError;
154 }
155
156 void CommandSender::cbNumberOfSinkClassesChanged()
157 {
158     CALL_ALL_INTERFACES(cbNumberOfSinkClassesChanged())
159 }
160
161 void CommandSender::cbNumberOfSourceClassesChanged()
162 {
163     CALL_ALL_INTERFACES(cbNumberOfSourceClassesChanged())
164 }
165
166 void CommandSender::cbMainConnectionStateChanged(const am_mainConnectionID_t connectionID, const am_ConnectionState_e connectionState)
167 {
168     CALL_ALL_INTERFACES(cbMainConnectionStateChanged(connectionID,connectionState))
169 }
170
171 void CommandSender::cbMainSinkSoundPropertyChanged(const am_sinkID_t sinkID, const am_MainSoundProperty_s& SoundProperty)
172 {
173     CALL_ALL_INTERFACES(cbMainSinkSoundPropertyChanged(sinkID,SoundProperty))
174 }
175
176 void CommandSender::cbMainSourceSoundPropertyChanged(const am_sourceID_t sourceID, const am_MainSoundProperty_s& SoundProperty)
177 {
178     CALL_ALL_INTERFACES(cbMainSourceSoundPropertyChanged(sourceID,SoundProperty))
179 }
180
181 void CommandSender::cbSinkAvailabilityChanged(const am_sinkID_t sinkID, const am_Availability_s & availability)
182 {
183     CALL_ALL_INTERFACES(cbSinkAvailabilityChanged(sinkID,availability))
184 }
185
186 void CommandSender::cbSourceAvailabilityChanged(const am_sourceID_t sourceID, const am_Availability_s & availability)
187 {
188     CALL_ALL_INTERFACES(cbSourceAvailabilityChanged(sourceID,availability))
189 }
190
191 void CommandSender::cbVolumeChanged(const am_sinkID_t sinkID, const am_mainVolume_t volume)
192 {
193     CALL_ALL_INTERFACES(cbVolumeChanged(sinkID,volume))
194 }
195
196 void CommandSender::cbSinkMuteStateChanged(const am_sinkID_t sinkID, const am_MuteState_e muteState)
197 {
198     CALL_ALL_INTERFACES(cbSinkMuteStateChanged(sinkID,muteState))
199 }
200
201 void CommandSender::cbSystemPropertyChanged(const am_SystemProperty_s & SystemProperty)
202 {
203     CALL_ALL_INTERFACES(cbSystemPropertyChanged(SystemProperty))
204 }
205
206 void CommandSender::cbTimingInformationChanged(const am_mainConnectionID_t mainConnection, const am_timeSync_t time)
207 {
208     CALL_ALL_INTERFACES(cbTimingInformationChanged(mainConnection,time))
209 }
210
211 void CommandSender::cbNewMainConnection(const am_MainConnectionType_s mainConnection)
212 {
213     CALL_ALL_INTERFACES(cbNewMainConnection(mainConnection))
214 }
215
216 void CommandSender::cbRemovedMainConnection(const am_mainConnectionID_t mainConnection)
217 {
218     CALL_ALL_INTERFACES(cbRemovedMainConnection(mainConnection))
219 }
220
221 void CommandSender::cbNewSink(const am_SinkType_s sink)
222 {
223     CALL_ALL_INTERFACES(cbNewSink(sink))
224 }
225
226 void CommandSender::cbRemovedSink(const am_sinkID_t sink)
227 {
228     CALL_ALL_INTERFACES(cbRemovedSink(sink))
229 }
230
231 void CommandSender::cbNewSource(const am_SourceType_s source)
232 {
233     CALL_ALL_INTERFACES(cbNewSource(source))
234 }
235
236 void CommandSender::cbRemovedSource(const am_sourceID_t source)
237 {
238     CALL_ALL_INTERFACES(cbRemovedSource(source))
239 }
240
241 void CommandSender::setCommandReady()
242 {
243     mCommandReceiver->waitOnStartup(false);
244     std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();
245     std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();
246     for (; iter < iterEnd; ++iter)
247     {
248         (*iter)->setCommandReady(mCommandReceiver->getStartupHandle());
249     }
250     mCommandReceiver->waitOnStartup(true);
251 }
252
253 void CommandSender::setCommandRundown()
254 {
255     mCommandReceiver->waitOnRundown(false);
256     std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();
257     std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();
258     for (; iter < iterEnd; ++iter)
259     {
260         (*iter)->setCommandRundown(mCommandReceiver->getRundownHandle());
261     }
262     mCommandReceiver->waitOnRundown(true);
263 }
264
265 void CommandSender::getInterfaceVersion(std::string & version) const
266 {
267     version = CommandSendVersion;
268 }
269
270 am_Error_e am::CommandSender::getListPlugins(std::vector<std::string> & interfaces) const
271 {
272     interfaces = mListLibraryNames;
273     return E_OK;
274 }
275
276 void CommandSender::unloadLibraries(void)
277 {
278     std::vector<void*>::iterator iterator = mListLibraryHandles.begin();
279     for (; iterator < mListLibraryHandles.end(); ++iterator)
280     {
281         dlclose(*iterator);
282     }
283     mListLibraryHandles.clear();
284 }
285