* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.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 "command/CommandReceiveInterface.h"
27 #include <dirent.h>
28 #include <dlt/dlt.h>
29 #include "PluginTemplate.h"
30 using namespace am;
31
32 #define REQUIRED_INTERFACE_VERSION 1
33
34 DLT_IMPORT_CONTEXT(AudioManager)
35
36 //!< macro to call all interfaces
37 #define CALL_ALL_INTERFACES(...)                                                                                                                 \
38                 std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();     \
39                 std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();    \
40                 for (; iter<iterEnd;++iter)                                                                                                              \
41                 {                                                                                                                                                                \
42                         (*iter)->__VA_ARGS__;                                                                                                            \
43                 }
44
45 CommandSender::CommandSender(const std::vector<std::string>& listOfPluginDirectories) :
46         mListInterfaces(), //
47         mListLibraryHandles()
48 {
49     std::vector<std::string> sharedLibraryNameList;
50     std::vector<std::string>::const_iterator dirIter = listOfPluginDirectories.begin();
51     std::vector<std::string>::const_iterator dirIterEnd = listOfPluginDirectories.end();
52
53     // search communicator plugins in configured directories
54     for (; dirIter < dirIterEnd; ++dirIter)
55     {
56         const char* directoryName = dirIter->c_str();
57         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Searching for CommandPlugins in"), DLT_STRING(directoryName));
58         DIR *directory = opendir(directoryName);
59
60         if (!directory)
61         {
62             DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Error opening directory "), DLT_STRING(directoryName));
63             continue;
64         }
65
66         // iterate content of directory
67         struct dirent *itemInDirectory = 0;
68         while ((itemInDirectory = readdir(directory)))
69         {
70             unsigned char entryType = itemInDirectory->d_type;
71             std::string entryName = itemInDirectory->d_name;
72
73             bool regularFile = (entryType == DT_REG || entryType == DT_LNK);
74             bool sharedLibExtension = ("so" == entryName.substr(entryName.find_last_of(".") + 1));
75
76             if (regularFile && sharedLibExtension)
77             {
78                 std::string name(directoryName);
79                 sharedLibraryNameList.push_back(name + "/" + entryName);
80             }
81         }
82         closedir(directory);
83     }
84
85     // iterate all communicator plugins and start them
86     std::vector<std::string>::iterator iter = sharedLibraryNameList.begin();
87     std::vector<std::string>::iterator iterEnd = sharedLibraryNameList.end();
88
89     for (; iter < iterEnd; ++iter)
90     {
91         DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Loading CommandSender plugin"), DLT_STRING(iter->c_str()));
92         CommandSendInterface* (*createFunc)();
93         void* tempLibHandle = NULL;
94         createFunc = getCreateFunction<CommandSendInterface*()>(*iter, tempLibHandle);
95
96         if (!createFunc)
97         {
98             DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("Entry point of CommandPlugin not found"), DLT_STRING(iter->c_str()));
99             continue;
100         }
101
102         CommandSendInterface* commander = createFunc();
103
104         if (!commander)
105         {
106             DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("CommandPlugin initialization failed. Entry Function not callable"));
107             continue;
108         }
109
110         //check libversion
111         if (commander->getInterfaceVersion() < REQUIRED_INTERFACE_VERSION)
112         {
113             DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("RoutingPlugin initialization failed. Version of Interface to old"));
114             continue;
115         }
116
117         mListInterfaces.push_back(commander);
118         mListLibraryHandles.push_back(tempLibHandle);
119     }
120 }
121
122 CommandSender::~CommandSender()
123 {
124     unloadLibraries();
125 }
126
127 am_Error_e CommandSender::stopInterface()
128 {
129     am_Error_e returnError = E_OK;
130
131     std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();
132     std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();
133     for (; iter < iterEnd; ++iter)
134     {
135         am_Error_e error = (*iter)->stopInterface();
136         if (error != E_OK)
137         {
138             returnError = error;
139         }
140     }
141     return returnError;
142 }
143
144 am_Error_e CommandSender::startupInterface(CommandReceiveInterface *commandreceiveinterface)
145 {
146     am_Error_e returnError = E_OK;
147
148     std::vector<CommandSendInterface*>::iterator iter = mListInterfaces.begin();
149     std::vector<CommandSendInterface*>::iterator iterEnd = mListInterfaces.end();
150     for (; iter < iterEnd; ++iter)
151     {
152         am_Error_e error = (*iter)->startupInterface(commandreceiveinterface);
153         if (error != E_OK)
154         {
155             returnError = error;
156         }
157     }
158     return returnError;
159 }
160
161 void CommandSender::cbCommunicationReady()
162 {
163     CALL_ALL_INTERFACES(cbCommunicationReady())
164 }
165
166 void CommandSender::cbCommunicationRundown()
167 {
168     CALL_ALL_INTERFACES(cbCommunicationRundown())
169 }
170
171 void CommandSender::cbNumberOfMainConnectionsChanged()
172 {
173     CALL_ALL_INTERFACES(cbNumberOfMainConnectionsChanged())
174 }
175
176 void CommandSender::cbNumberOfSinksChanged()
177 {
178     CALL_ALL_INTERFACES(cbNumberOfSinksChanged())
179 }
180
181 void CommandSender::cbNumberOfSourcesChanged()
182 {
183     CALL_ALL_INTERFACES(cbNumberOfSourcesChanged())
184 }
185
186 void CommandSender::cbNumberOfSinkClassesChanged()
187 {
188     CALL_ALL_INTERFACES(cbNumberOfSinkClassesChanged())
189 }
190
191 void CommandSender::cbNumberOfSourceClassesChanged()
192 {
193     CALL_ALL_INTERFACES(cbNumberOfSourceClassesChanged())
194 }
195
196 void CommandSender::cbMainConnectionStateChanged(const am_mainConnectionID_t connectionID, const am_ConnectionState_e connectionState)
197 {
198     CALL_ALL_INTERFACES(cbMainConnectionStateChanged(connectionID,connectionState))
199 }
200
201 void CommandSender::cbMainSinkSoundPropertyChanged(const am_sinkID_t sinkID, const am_MainSoundProperty_s SoundProperty)
202 {
203     CALL_ALL_INTERFACES(cbMainSinkSoundPropertyChanged(sinkID,SoundProperty))
204 }
205
206 void CommandSender::cbMainSourceSoundPropertyChanged(const am_sourceID_t sourceID, const am_MainSoundProperty_s & SoundProperty)
207 {
208     CALL_ALL_INTERFACES(cbMainSourceSoundPropertyChanged(sourceID,SoundProperty))
209 }
210
211 void CommandSender::cbSinkAvailabilityChanged(const am_sinkID_t sinkID, const am_Availability_s & availability)
212 {
213     CALL_ALL_INTERFACES(cbSinkAvailabilityChanged(sinkID,availability))
214 }
215
216 void CommandSender::cbSourceAvailabilityChanged(const am_sourceID_t sourceID, const am_Availability_s & availability)
217 {
218     CALL_ALL_INTERFACES(cbSourceAvailabilityChanged(sourceID,availability))
219 }
220
221 void CommandSender::cbVolumeChanged(const am_sinkID_t sinkID, const am_mainVolume_t volume)
222 {
223     CALL_ALL_INTERFACES(cbVolumeChanged(sinkID,volume))
224 }
225
226 void CommandSender::cbSinkMuteStateChanged(const am_sinkID_t sinkID, const am_MuteState_e muteState)
227 {
228     CALL_ALL_INTERFACES(cbSinkMuteStateChanged(sinkID,muteState))
229 }
230
231 void CommandSender::cbSystemPropertyChanged(const am_SystemProperty_s & SystemProperty)
232 {
233     CALL_ALL_INTERFACES(cbSystemPropertyChanged(SystemProperty))
234 }
235
236 void CommandSender::cbTimingInformationChanged(const am_mainConnectionID_t mainConnection, const am_timeSync_t time)
237 {
238     CALL_ALL_INTERFACES(cbTimingInformationChanged(mainConnection,time))
239 }
240
241 void CommandSender::unloadLibraries(void)
242 {
243     std::vector<void*>::iterator iterator = mListLibraryHandles.begin();
244     for (; iterator < mListLibraryHandles.end(); ++iterator)
245     {
246         dlclose(*iterator);
247     }
248     mListLibraryHandles.clear();
249 }
250
251 uint16_t CommandSender::getInterfaceVersion() const
252 {
253     return CommandSendVersion;
254 }
255