* formatting all the source code with eclipse source code style
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / test / CommonFunctions.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file CommonFunctions.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 "CommonFunctions.h"
27 #include "CommandInterfaceBackdoor.h"
28 #include "RoutingInterfaceBackdoor.h"
29 #include "ControlInterfaceBackdoor.h"
30 #include <assert.h>
31 #include <sstream>
32
33 using namespace am;
34
35 CommandInterfaceBackdoor::CommandInterfaceBackdoor()
36 {
37 }
38 CommandInterfaceBackdoor::~CommandInterfaceBackdoor()
39 {
40 }
41
42 bool CommandInterfaceBackdoor::unloadPlugins(CommandSender *CommandSender)
43 {
44     assert(CommandSender != NULL);
45     CommandSender->unloadLibraries();
46     CommandSender->mListInterfaces.clear();
47     if (CommandSender->mListInterfaces.empty())
48         return true;
49     return false;
50 }
51
52 bool CommandInterfaceBackdoor::injectInterface(CommandSender *CommandSender, CommandSendInterface *CommandSendInterface)
53 {
54     assert(CommandSender != NULL);
55     assert(CommandSendInterface != NULL);
56     CommandSender->mListInterfaces.push_back(CommandSendInterface);
57     return true; //todo: check if it worked
58 }
59
60 RoutingInterfaceBackdoor::RoutingInterfaceBackdoor()
61 {
62 }
63 RoutingInterfaceBackdoor::~RoutingInterfaceBackdoor()
64 {
65 }
66
67 bool RoutingInterfaceBackdoor::unloadPlugins(RoutingSender *RoutingSender)
68 {
69     assert(RoutingSender != NULL);
70     RoutingSender->unloadLibraries();
71     RoutingSender->mListInterfaces.clear();
72     if (RoutingSender->mListInterfaces.empty())
73         return true;
74     return false;
75 }
76
77 bool RoutingInterfaceBackdoor::injectInterface(RoutingSender *RoutingSender, RoutingSendInterface *newInterface, const std::string& busname)
78 {
79     assert(RoutingSender != NULL);
80     assert(newInterface != NULL);
81
82     RoutingSender::InterfaceNamePairs newInterfacePair;
83     newInterfacePair.routingInterface = newInterface;
84     newInterfacePair.busName = busname;
85     RoutingSender->mListInterfaces.push_back(newInterfacePair);
86     return true; //todo: check if it worked
87 }
88
89 ControlInterfaceBackdoor::ControlInterfaceBackdoor()
90 {
91 }
92
93 ControlInterfaceBackdoor::~ControlInterfaceBackdoor()
94 {
95 }
96
97 bool ControlInterfaceBackdoor::replaceController(ControlSender *controlSender, ControlSendInterface *newController)
98 {
99     controlSender->mController = newController;
100     return true;
101 }
102
103 //int GetRandomNumber(int nLow, int nHigh) {
104 //      return (rand() % (nHigh - nLow + 1)) + nLow;
105 //}
106
107 bool equalSoundProperty(const am_SoundProperty_s a, const am_SoundProperty_s b)
108 {
109     return (a.type == b.type && a.value == b.value);
110 }
111
112 bool equalMainSoundProperty(const am_MainSoundProperty_s a, const am_MainSoundProperty_s b)
113 {
114     return (a.type == b.type && a.value == b.value);
115 }
116
117 bool equalRoutingElement(const am_RoutingElement_s a, const am_RoutingElement_s b)
118 {
119     return (a.connectionFormat == b.connectionFormat && a.domainID == b.domainID && a.sinkID == b.sinkID && a.sourceID == b.sourceID);
120 }
121
122 bool equalClassProperties(const am_ClassProperty_s a, const am_ClassProperty_s b)
123 {
124     return (a.classProperty == b.classProperty && a.value == b.value);
125 }
126
127 std::string int2string(int i)
128 {
129     std::stringstream out;
130     out << i;
131     return out.str();
132 }
133
134 bool CommonFunctions::compareSource(std::vector<am_Source_s>::iterator listIterator, const am_Source_s& sourceData)
135 {
136     return (listIterator->available.availability == sourceData.available.availability) && (listIterator->available.availabilityReason == sourceData.available.availabilityReason) && (listIterator->sourceClassID == sourceData.sourceClassID) && (listIterator->domainID == sourceData.domainID) && (listIterator->interruptState == sourceData.interruptState) && (listIterator->visible == sourceData.visible) && (listIterator->name.compare(sourceData.name) == 0) && (listIterator->volume == sourceData.volume) && std::equal(listIterator->listConnectionFormats.begin(), listIterator->listConnectionFormats.end(), sourceData.listConnectionFormats.begin())
137             && std::equal(listIterator->listMainSoundProperties.begin(), listIterator->listMainSoundProperties.end(), sourceData.listMainSoundProperties.begin(), equalMainSoundProperty) && std::equal(listIterator->listSoundProperties.begin(), listIterator->listSoundProperties.end(), sourceData.listSoundProperties.begin(), equalSoundProperty);
138 }
139
140 bool CommonFunctions::compareSink(std::vector<am_Sink_s>::iterator listIterator, const am_Sink_s& sinkData)
141 {
142     return (listIterator->available.availability == sinkData.available.availability) && (listIterator->available.availabilityReason == sinkData.available.availabilityReason) && (listIterator->sinkClassID == sinkData.sinkClassID) && (listIterator->domainID == sinkData.domainID) && (listIterator->mainVolume == sinkData.mainVolume) && (listIterator->muteState == sinkData.muteState) && (listIterator->visible == sinkData.visible) && (listIterator->name.compare(sinkData.name) == 0) && (listIterator->volume == sinkData.volume) && std::equal(listIterator->listConnectionFormats.begin(), listIterator->listConnectionFormats.end(), sinkData.listConnectionFormats.begin())
143             && std::equal(listIterator->listMainSoundProperties.begin(), listIterator->listMainSoundProperties.end(), sinkData.listMainSoundProperties.begin(), equalMainSoundProperty) && std::equal(listIterator->listSoundProperties.begin(), listIterator->listSoundProperties.end(), sinkData.listSoundProperties.begin(), equalSoundProperty);
144 }
145
146 bool CommonFunctions::compareGateway(std::vector<am_Gateway_s>::iterator listIterator, const am_Gateway_s& gatewayData)
147 {
148     return (listIterator->name.compare(gatewayData.name) == 0) && (listIterator->sinkID == gatewayData.sinkID) && (listIterator->sourceID == gatewayData.sourceID) && (listIterator->controlDomainID == gatewayData.controlDomainID) && (listIterator->domainSinkID == gatewayData.domainSinkID) && (listIterator->domainSourceID == gatewayData.domainSourceID) && std::equal(listIterator->convertionMatrix.begin(), listIterator->convertionMatrix.end(), gatewayData.convertionMatrix.begin()) && std::equal(listIterator->listSourceFormats.begin(), listIterator->listSourceFormats.end(), gatewayData.listSourceFormats.begin()) && std::equal(listIterator->listSinkFormats.begin(), listIterator->listSinkFormats.end(), gatewayData.listSinkFormats.begin());
149 }
150
151 bool CommonFunctions::compareGateway1(const am_Gateway_s gateway1, const am_Gateway_s gatewayData)
152 {
153     return (gateway1.name.compare(gatewayData.name) == 0) && (gateway1.sinkID == gatewayData.sinkID) && (gateway1.sourceID == gatewayData.sourceID) && (gateway1.controlDomainID == gatewayData.controlDomainID) && (gateway1.domainSinkID == gatewayData.domainSinkID) && (gateway1.domainSourceID == gatewayData.domainSourceID) && std::equal(gateway1.convertionMatrix.begin(), gateway1.convertionMatrix.end(), gatewayData.convertionMatrix.begin()) && std::equal(gateway1.listSourceFormats.begin(), gateway1.listSourceFormats.end(), gatewayData.listSourceFormats.begin()) && std::equal(gateway1.listSinkFormats.begin(), gateway1.listSinkFormats.end(), gatewayData.listSinkFormats.begin());
154 }
155
156 bool CommonFunctions::compareSinkMainSink(std::vector<am_SinkType_s>::iterator listIterator, const std::vector<am_Sink_s>& sinkList)
157 {
158     std::vector<am_Sink_s>::const_iterator sinkListIterator = sinkList.begin();
159     for (; sinkListIterator < sinkList.end(); ++sinkListIterator)
160     {
161         if (listIterator->sinkID == sinkListIterator->sinkID)
162         {
163             return (listIterator->name.compare(sinkListIterator->name) == 0) && (listIterator->availability.availability == sinkListIterator->available.availability) && (listIterator->availability.availabilityReason == sinkListIterator->available.availabilityReason) && (listIterator->muteState == sinkListIterator->muteState) && (listIterator->volume == sinkListIterator->mainVolume) && (listIterator->sinkClassID == sinkListIterator->sinkClassID);
164         }
165     }
166     return false;
167 }
168
169 bool CommonFunctions::compareSinkMainSource(std::vector<am_SourceType_s>::iterator listIterator, const std::vector<am_Source_s>& sourceList)
170 {
171     std::vector<am_Source_s>::const_iterator sinkListIterator = sourceList.begin();
172     for (; sinkListIterator < sourceList.end(); ++sinkListIterator)
173     {
174         if (listIterator->sourceID == sinkListIterator->sourceID)
175         {
176             return (listIterator->name.compare(sinkListIterator->name) == 0) && (listIterator->availability.availability == sinkListIterator->available.availability) && (listIterator->availability.availabilityReason == sinkListIterator->available.availabilityReason) && (listIterator->sourceClassID == sinkListIterator->sourceClassID);
177         }
178     }
179     return false;
180 }
181
182 bool CommonFunctions::compareRoute(am_Route_s a, am_Route_s b)
183 {
184     bool retVal = true;
185     std::vector<am_RoutingElement_s>::iterator itA = a.route.begin(), itB = b.route.begin();
186     retVal &= a.sourceID == b.sourceID;
187     retVal &= a.sinkID == b.sinkID;
188     for (; itA != a.route.end(); ++itA)
189     {
190         retVal &= itA->sinkID == itB->sinkID;
191         retVal &= itA->sourceID == itB->sourceID;
192         retVal &= itA->connectionFormat == itB->connectionFormat;
193         retVal &= itA->domainID == itB->domainID;
194         itB++;
195     }
196     return retVal;
197 }
198
199 std::vector<am_ConnectionFormat_e> CommonFunctions::getStandardConnectionFormatList()
200 {
201     std::vector<am_ConnectionFormat_e> list;
202     list.push_back(CF_ANALOG);
203     list.push_back(CF_STEREO);
204     return list;
205 }
206
207 std::vector<am_SoundProperty_s> CommonFunctions::getStandardSoundPropertyList()
208 {
209     std::vector<am_SoundProperty_s> soundPropertyList;
210     am_SoundProperty_s soundProperty;
211     soundProperty.type = SP_BASS;
212     soundProperty.value = 23;
213     soundPropertyList.push_back(soundProperty);
214     soundProperty.type = SP_MID;
215     soundProperty.value = 2;
216     soundPropertyList.push_back(soundProperty);
217     return soundPropertyList;
218 }
219
220 std::vector<am_MainSoundProperty_s> CommonFunctions::getStandardMainSoundPropertyList()
221 {
222     std::vector<am_MainSoundProperty_s> mainSoundPropertyList;
223     am_MainSoundProperty_s mainSoundProperty;
224     mainSoundProperty.type = MSP_NAVIGATION_OFFSET;
225     mainSoundProperty.value = 23;
226     mainSoundPropertyList.push_back(mainSoundProperty);
227     mainSoundProperty.type = MSP_TEST;
228     mainSoundProperty.value = 3;
229     mainSoundPropertyList.push_back(mainSoundProperty);
230     return mainSoundPropertyList;
231 }
232
233 void CommonFunctions::createSink(am_Sink_s& sink) const
234 {
235     sink.name = "AnySink";
236     sink.domainID = 4;
237     sink.available.availability = A_AVAILABLE;
238     sink.available.availabilityReason = AR_NEWMEDIA;
239     sink.sinkClassID = 1;
240     sink.listConnectionFormats = getStandardConnectionFormatList();
241     sink.listSoundProperties = getStandardSoundPropertyList();
242     sink.listMainSoundProperties = getStandardMainSoundPropertyList();
243     sink.mainVolume = 12;
244     sink.muteState = MS_UNMUTED;
245     sink.visible = true;
246     sink.volume = 23;
247     sink.sinkID = 0;
248 }
249
250 void CommonFunctions::createSource(am_Source_s& source) const
251 {
252     source.name = "AnySource";
253     source.domainID = 4;
254     source.available.availability = A_AVAILABLE;
255     source.available.availabilityReason = AR_NEWMEDIA;
256     source.sourceClassID = 1;
257     source.listConnectionFormats = getStandardConnectionFormatList();
258     source.listSoundProperties = getStandardSoundPropertyList();
259     source.listMainSoundProperties = getStandardMainSoundPropertyList();
260     source.interruptState = IS_OFF;
261     source.visible = true;
262     source.volume = 23;
263     source.sourceID = 0;
264     source.sourceState = SS_ON;
265 }
266
267 void CommonFunctions::createDomain(am_Domain_s & domain) const
268 {
269
270     domain.domainID = 0;
271     domain.name = "AnyDomain";
272     domain.nodename = "AnyNode";
273     domain.busname = "AnyBusname";
274     domain.complete = true;
275     domain.early = true;
276     domain.state = DS_CONTROLLED;
277 }
278
279 void CommonFunctions::createGateway(am_Gateway_s & gateway)
280 {
281     gateway.name = "AnyGateway";
282     gateway.sinkID = 1;
283     gateway.sourceID = 2;
284     gateway.controlDomainID = 1;
285     gateway.domainSinkID = 3;
286     gateway.domainSourceID = 4;
287     gateway.convertionMatrix = getStandardConvertionMatrix();
288     gateway.listSourceFormats = getStandardConnectionFormatList();
289     gateway.listSinkFormats = getStandardConnectionFormatList();
290     gateway.gatewayID = 0;
291
292 }
293
294 void CommonFunctions::createConnection(am_Connection_s & connection) const
295 {
296     connection.connectionID = 0;
297     connection.sinkID = 1;
298     connection.sourceID = 2;
299     connection.delay = -1;
300     connection.connectionFormat = CF_ANALOG;
301 }
302
303 void CommonFunctions::createMainConnection(am_MainConnection_s & mainConnection, am_Route_s route) const
304 {
305     mainConnection.connectionID = 0;
306     mainConnection.connectionState = CS_CONNECTED;
307     mainConnection.route = route;
308     mainConnection.delay = -1;
309 }
310
311 std::vector<bool> CommonFunctions::getStandardConvertionMatrix()
312 {
313     std::vector<bool> convMatrix;
314     convMatrix.push_back(true);
315     convMatrix.push_back(false);
316     convMatrix.push_back(true);
317     convMatrix.push_back(false);
318     convMatrix.push_back(true);
319     convMatrix.push_back(true);
320     return convMatrix;
321 }
322
323 void CommonFunctions::connectionList2RoutingList(std::vector<am_RoutingElement_s> & routingList, const std::vector<am_Connection_s>& connectionList)
324 {
325     am_RoutingElement_s routingElement;
326     std::vector<am_Connection_s>::const_iterator cIterator = connectionList.begin();
327     for (; cIterator < connectionList.end(); ++cIterator)
328     {
329         routingElement.sinkID = cIterator->sinkID;
330         routingElement.sourceID = cIterator->sourceID;
331         routingElement.connectionFormat = cIterator->connectionFormat;
332         routingElement.domainID = 4; //todo: make this test read out the real value
333         routingList.push_back(routingElement);
334     }
335 }
336