2 * Copyright (C) 2012, BMW AG
4 * This file is part of GENIVI Project AudioManager.
6 * Contributions are licensed to the GENIVI Alliance under one or more
7 * Contribution License Agreements.
10 * This Source Code Form is subject to the terms of the
11 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
12 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
17 * \file CAmCommandSender.cpp
18 * For further information see http://www.genivi.org/.
22 #include "CAmCommandSender.h"
26 #include "CAmCommandReceiver.h"
27 #include "TAmPluginTemplate.h"
28 #include "shared/CAmDltWrapper.h"
33 #define REQUIRED_INTERFACE_VERSION_MAJOR 1 //!< major interface version. All versions smaller than this will be rejected
34 #define REQUIRED_INTERFACE_VERSION_MINOR 0 //!< minor interface version. All versions smaller than this will be rejected
37 * macro to call all interfaces
39 #define CALL_ALL_INTERFACES(...) \
40 std::vector<IAmCommandSend*>::iterator iter = mListInterfaces.begin(); \
41 std::vector<IAmCommandSend*>::iterator iterEnd = mListInterfaces.end(); \
42 for (; iter<iterEnd;++iter) \
44 (*iter)->__VA_ARGS__; \
47 CAmCommandSender::CAmCommandSender(const std::vector<std::string>& listOfPluginDirectories) :
49 mListLibraryHandles(), //
50 mListLibraryNames(), //
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();
57 // search communicator plugins in configured directories
58 for (; dirIter < dirIterEnd; ++dirIter)
60 const char* directoryName = dirIter->c_str();
61 logInfo("Searching for CommandPlugins in", *dirIter);
62 DIR *directory = opendir(directoryName);
66 logError("Error opening directory ", *dirIter);
70 // iterate content of directory
71 struct dirent *itemInDirectory = 0;
72 while ((itemInDirectory = readdir(directory)))
74 unsigned char entryType = itemInDirectory->d_type;
75 std::string entryName = itemInDirectory->d_name;
77 bool regularFile = (entryType == DT_REG || entryType == DT_LNK);
78 bool sharedLibExtension = ("so" == entryName.substr(entryName.find_last_of(".") + 1));
80 if (regularFile && sharedLibExtension)
82 std::string name(directoryName);
83 sharedLibraryNameList.push_back(name + "/" + entryName);
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();
93 for (; iter < iterEnd; ++iter)
95 logInfo("Loading CommandSender plugin", *iter);
96 IAmCommandSend* (*createFunc)();
97 void* tempLibHandle = NULL;
98 createFunc = getCreateFunction<IAmCommandSend*()>(*iter, tempLibHandle);
102 logInfo("Entry point of CommandPlugin not found", *iter);
106 IAmCommandSend* commander = createFunc();
110 logInfo("CommandPlugin initialization failed. Entry Function not callable");
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;
121 if (majorVersion < REQUIRED_INTERFACE_VERSION_MAJOR || ((majorVersion == REQUIRED_INTERFACE_VERSION_MAJOR) && (minorVersion > REQUIRED_INTERFACE_VERSION_MINOR)))
123 logInfo("CommandInterface initialization failed. Version of Interface to old");
127 mListInterfaces.push_back(commander);
128 mListLibraryHandles.push_back(tempLibHandle);
129 mListLibraryNames.push_back(iter->c_str());
133 CAmCommandSender::~CAmCommandSender()
138 am_Error_e CAmCommandSender::startupInterfaces(CAmCommandReceiver *iCommandReceiver)
140 mCommandReceiver = iCommandReceiver;
141 am_Error_e returnError = E_OK;
143 std::vector<IAmCommandSend*>::iterator iter = mListInterfaces.begin();
144 std::vector<IAmCommandSend*>::iterator iterEnd = mListInterfaces.end();
145 for (; iter < iterEnd; ++iter)
147 am_Error_e error = (*iter)->startupInterface(iCommandReceiver);
153 return (returnError);
156 void CAmCommandSender::cbNumberOfSinkClassesChanged()
158 CALL_ALL_INTERFACES(cbNumberOfSinkClassesChanged())
161 void CAmCommandSender::cbNumberOfSourceClassesChanged()
163 CALL_ALL_INTERFACES(cbNumberOfSourceClassesChanged())
166 void CAmCommandSender::cbMainConnectionStateChanged(const am_mainConnectionID_t connectionID, const am_ConnectionState_e connectionState)
168 CALL_ALL_INTERFACES(cbMainConnectionStateChanged(connectionID,connectionState))
171 void CAmCommandSender::cbMainSinkSoundPropertyChanged(const am_sinkID_t sinkID, const am_MainSoundProperty_s& SoundProperty)
173 CALL_ALL_INTERFACES(cbMainSinkSoundPropertyChanged(sinkID,SoundProperty))
176 void CAmCommandSender::cbMainSourceSoundPropertyChanged(const am_sourceID_t sourceID, const am_MainSoundProperty_s& SoundProperty)
178 CALL_ALL_INTERFACES(cbMainSourceSoundPropertyChanged(sourceID,SoundProperty))
181 void CAmCommandSender::cbSinkAvailabilityChanged(const am_sinkID_t sinkID, const am_Availability_s & availability)
183 CALL_ALL_INTERFACES(cbSinkAvailabilityChanged(sinkID,availability))
186 void CAmCommandSender::cbSourceAvailabilityChanged(const am_sourceID_t sourceID, const am_Availability_s & availability)
188 CALL_ALL_INTERFACES(cbSourceAvailabilityChanged(sourceID,availability))
191 void CAmCommandSender::cbVolumeChanged(const am_sinkID_t sinkID, const am_mainVolume_t volume)
193 CALL_ALL_INTERFACES(cbVolumeChanged(sinkID,volume))
196 void CAmCommandSender::cbSinkMuteStateChanged(const am_sinkID_t sinkID, const am_MuteState_e muteState)
198 CALL_ALL_INTERFACES(cbSinkMuteStateChanged(sinkID,muteState))
201 void CAmCommandSender::cbSystemPropertyChanged(const am_SystemProperty_s & SystemProperty)
203 CALL_ALL_INTERFACES(cbSystemPropertyChanged(SystemProperty))
206 void CAmCommandSender::cbTimingInformationChanged(const am_mainConnectionID_t mainConnection, const am_timeSync_t time)
208 CALL_ALL_INTERFACES(cbTimingInformationChanged(mainConnection,time))
211 void CAmCommandSender::cbNewMainConnection(const am_MainConnectionType_s mainConnection)
213 CALL_ALL_INTERFACES(cbNewMainConnection(mainConnection))
216 void CAmCommandSender::cbRemovedMainConnection(const am_mainConnectionID_t mainConnection)
218 CALL_ALL_INTERFACES(cbRemovedMainConnection(mainConnection))
221 void CAmCommandSender::cbNewSink(const am_SinkType_s sink)
223 CALL_ALL_INTERFACES(cbNewSink(sink))
226 void CAmCommandSender::cbRemovedSink(const am_sinkID_t sink)
228 CALL_ALL_INTERFACES(cbRemovedSink(sink))
231 void CAmCommandSender::cbNewSource(const am_SourceType_s source)
233 CALL_ALL_INTERFACES(cbNewSource(source))
236 void CAmCommandSender::cbRemovedSource(const am_sourceID_t source)
238 CALL_ALL_INTERFACES(cbRemovedSource(source))
241 void CAmCommandSender::setCommandReady()
243 mCommandReceiver->waitOnStartup(false);
244 std::vector<IAmCommandSend*>::iterator iter = mListInterfaces.begin();
245 std::vector<IAmCommandSend*>::iterator iterEnd = mListInterfaces.end();
246 for (; iter < iterEnd; ++iter)
248 (*iter)->setCommandReady(mCommandReceiver->getStartupHandle());
250 mCommandReceiver->waitOnStartup(true);
253 void CAmCommandSender::setCommandRundown()
255 mCommandReceiver->waitOnRundown(false);
256 std::vector<IAmCommandSend*>::iterator iter = mListInterfaces.begin();
257 std::vector<IAmCommandSend*>::iterator iterEnd = mListInterfaces.end();
258 for (; iter < iterEnd; ++iter)
260 (*iter)->setCommandRundown(mCommandReceiver->getRundownHandle());
262 mCommandReceiver->waitOnRundown(true);
265 void CAmCommandSender::getInterfaceVersion(std::string & version) const
267 version = CommandSendVersion;
270 am_Error_e am::CAmCommandSender::getListPlugins(std::vector<std::string> & interfaces) const
272 interfaces = mListLibraryNames;
276 void CAmCommandSender::unloadLibraries(void)
278 std::vector<void*>::iterator iterator = mListLibraryHandles.begin();
279 for (; iterator < mListLibraryHandles.end(); ++iterator)
283 mListLibraryHandles.clear();