* wrapping DLT calls in a new Class because of performance, codesize and lazyness...
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / Router.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file Router.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 "Router.h"
26 #include "DatabaseHandler.h"
27 #include "ControlSender.h"
28 #include <assert.h>
29 #include <algorithm>
30 #include <vector>
31 #include <iterator>
32
33 using namespace am;
34
35 Router::Router(DatabaseHandler* iDatabaseHandler, ControlSender* iSender) :
36         mDatabaseHandler(iDatabaseHandler), //
37         mControlSender(iSender)
38 {
39     assert(mDatabaseHandler);
40     assert(mControlSender);
41 }
42
43 am_Error_e Router::getRoute(const bool onlyfree, const am_sourceID_t sourceID, const am_sinkID_t sinkID, std::vector<am_Route_s> & returnList)
44 {
45     //first find out in which domains the source and sink are
46     am_domainID_t sourceDomainID;
47     am_domainID_t sinkDomainID;
48     if (mDatabaseHandler->getDomainOfSource(sourceID, sourceDomainID) != E_OK)
49         return (E_NON_EXISTENT);
50     if (mDatabaseHandler->getDomainOfSink(sinkID, sinkDomainID) != E_OK)
51         return (E_NON_EXISTENT);
52
53     RoutingTree routingtree(sourceDomainID); //Build up a Tree from the Source_Domain to every other domain.
54     std::vector<RoutingTreeItem*> flattree; //This list is the flat tree
55     std::vector<RoutingTreeItem*> matchtree;
56     std::vector<am_gatewayID_t> listGatewayID; //holds all gateway ids of the route
57     am_RoutingElement_s routingElement;
58     am_Route_s actualRoute; //holds the actual Route
59     am_sourceID_t lastSource = 0;
60
61     //TODO: kind of unclean. The separation between database and router could be better.
62     mDatabaseHandler->getRoutingTree(onlyfree, routingtree, flattree); //Build up the tree out of the database as
63
64     //we go through the returned flattree and look for our sink, after that flattree holds only treeItems that match
65     std::vector<RoutingTreeItem*>::iterator iterator = flattree.begin();
66     for (; iterator != flattree.end(); ++iterator)
67     {
68         if ((*iterator)->returnDomainID() == sinkDomainID)
69         {
70             matchtree.push_back(*iterator);
71         }
72     }
73
74     //No we need to trace back the routes for each entry in matchtree
75     iterator = matchtree.begin();
76     for (; iterator != matchtree.end(); ++iterator)
77     {
78         std::vector<am_RoutingElement_s> actualRoutingElement; //intermediate list of current routing pairs
79         //getting the route for the actual item
80         routingtree.getRoute(*iterator, listGatewayID); //This gives only the Gateway IDs we need more
81
82         //go throught the gatewayids and get more information
83         std::vector<am_gatewayID_t>::iterator gatewayIterator = listGatewayID.begin();
84         for (; gatewayIterator != listGatewayID.end(); ++gatewayIterator)
85         {
86             am_Gateway_s gatewayData;
87             if (mDatabaseHandler->getGatewayInfoDB(*gatewayIterator, gatewayData) != E_OK)
88                 return (E_UNKNOWN);
89
90             //at the beginning of the route, we connect first the source to the first gateway
91             if (gatewayIterator == listGatewayID.begin())
92             {
93                 routingElement.sourceID = sourceID;
94                 routingElement.domainID = sourceDomainID;
95             }
96             else
97             {
98                 routingElement.sourceID = lastSource;
99                 routingElement.domainID = gatewayData.domainSinkID;
100             }
101             routingElement.sinkID = gatewayData.sinkID;
102             actualRoutingElement.push_back(routingElement);
103             lastSource = gatewayData.sourceID;
104         }
105         //at the end of the route, connect to the sink !
106         routingElement.sourceID = lastSource;
107         routingElement.sinkID = sinkID;
108         routingElement.domainID = sinkDomainID;
109         actualRoutingElement.push_back(routingElement);
110
111         //So now we got the route, what is missing are the connectionFormats...
112
113         //Step through the routes and try to use always the best connectionFormat
114         std::vector<am_RoutingElement_s>::iterator routingInterator = actualRoutingElement.begin();
115         gatewayIterator = listGatewayID.begin();
116         if (findBestWay(actualRoutingElement, routingInterator, gatewayIterator) != E_OK)
117         {
118             continue;
119         }
120
121         //add the route to the list of routes...
122         actualRoute.sourceID = sourceID;
123         actualRoute.sinkID = sinkID;
124         actualRoute.route = actualRoutingElement;
125         returnList.push_back(actualRoute);
126     }
127     return (E_OK);
128 }
129
130 void Router::listPossibleConnectionFormats(const am_sourceID_t sourceID, const am_sinkID_t sinkID, std::vector<am_ConnectionFormat_e>& listFormats) const
131 {
132     std::vector<am_ConnectionFormat_e> listSourceFormats;
133     std::vector<am_ConnectionFormat_e> listSinkFormats;
134     mDatabaseHandler->getListSinkConnectionFormats(sinkID, listSinkFormats);
135     mDatabaseHandler->getListSourceConnectionFormats(sourceID, listSourceFormats);
136     std::sort(listSinkFormats.begin(),listSinkFormats.end()); //todo: this might be not needed if we use strictly sorted input
137     std::sort(listSourceFormats.begin(),listSourceFormats.end()); //todo: this might be not needed if we use strictly sorted input
138     std::insert_iterator<std::vector<am_ConnectionFormat_e> > inserter(listFormats, listFormats.begin());
139     set_intersection(listSourceFormats.begin(), listSourceFormats.end(), listSinkFormats.begin(), listSinkFormats.end(), inserter);
140 }
141
142 am_Error_e Router::findBestWay(std::vector<am_RoutingElement_s> & listRoute, std::vector<am_RoutingElement_s>::iterator routeIterator, std::vector<am_gatewayID_t>::iterator gatewayIterator)
143 {
144     am_Error_e returnError = E_NOT_POSSIBLE;
145     std::vector<am_ConnectionFormat_e> listConnectionFormats;
146     std::vector<am_ConnectionFormat_e> listMergeConnectionFormats;
147     std::vector<am_ConnectionFormat_e> listPriorityConnectionFormats;
148     std::vector<am_RoutingElement_s>::iterator nextIterator = routeIterator + 1;
149     //get best connection format
150     listPossibleConnectionFormats(routeIterator->sourceID, routeIterator->sinkID, listConnectionFormats);
151
152     //if we have not just started, we need to take care about the gateways...
153     if (routeIterator != listRoute.begin())
154     {
155         //since we have to deal with Gateways, there are restrictions what connectionFormat we can take. So we need to take the subset of connections that are restricted:
156         std::vector<am_ConnectionFormat_e> listRestrictedConnectionFormats;
157         std::insert_iterator<std::vector<am_ConnectionFormat_e> > inserter(listMergeConnectionFormats, listMergeConnectionFormats.begin());
158         std::vector<am_RoutingElement_s>::iterator tempIterator(routeIterator);
159         tempIterator--;
160         listRestrictedOutputFormatsGateways(*gatewayIterator, (tempIterator)->connectionFormat, listRestrictedConnectionFormats);
161         std::sort(listRestrictedConnectionFormats.begin(),listRestrictedConnectionFormats.end());           //todo: this might be not needed if we use strictly sorted input
162         set_intersection(listConnectionFormats.begin(), listConnectionFormats.end(), listRestrictedConnectionFormats.begin(), listRestrictedConnectionFormats.end(), inserter);
163         gatewayIterator++;
164     }
165     else
166     {
167         listMergeConnectionFormats = listConnectionFormats;
168     }
169
170     //let the controller decide:
171     mControlSender->getConnectionFormatChoice(routeIterator->sourceID, routeIterator->sinkID, listMergeConnectionFormats, listPriorityConnectionFormats);
172
173     //we have the list sorted after prios - now we try one after the other with the next part of the route
174     std::vector<am_ConnectionFormat_e>::iterator connectionFormatIterator = listPriorityConnectionFormats.begin();
175
176     //here we need to check if we are at the end and stop
177     if (nextIterator == listRoute.end())
178     {
179         if (!listPriorityConnectionFormats.empty())
180         {
181             routeIterator->connectionFormat = listPriorityConnectionFormats.front();
182             return E_OK;
183         }
184         else
185             return E_NOT_POSSIBLE;
186     }
187
188     for (; connectionFormatIterator != listPriorityConnectionFormats.end(); ++connectionFormatIterator)
189     {
190         routeIterator->connectionFormat = *connectionFormatIterator;
191         if ((returnError = findBestWay(listRoute, nextIterator, gatewayIterator)) == E_OK)
192         {
193             break;
194         }
195     }
196
197     return (returnError);
198 }
199
200 void Router::listRestrictedOutputFormatsGateways(const am_gatewayID_t gatewayID, const am_ConnectionFormat_e sinkConnectionFormat, std::vector<am_ConnectionFormat_e> & listFormats) const
201 {
202     listFormats.clear();
203     am_Gateway_s gatewayData;
204     mDatabaseHandler->getGatewayInfoDB(gatewayID, gatewayData);
205     std::vector<am_ConnectionFormat_e>::const_iterator rowSinkIterator = gatewayData.listSinkFormats.begin();
206     std::vector<bool>::const_iterator matrixIterator = gatewayData.convertionMatrix.begin();
207
208     //find the row number of the sink
209     rowSinkIterator = find(gatewayData.listSinkFormats.begin(), gatewayData.listSinkFormats.end(), sinkConnectionFormat);
210     int rowNumberSink = rowSinkIterator - gatewayData.listSinkFormats.begin();
211
212     //go through the convertionMatrix and find out if the conversion is possible, if yes, add connectionFormat ...
213     std::advance(matrixIterator, rowNumberSink);
214
215     //iterate line-wise through the matrix and add more formats
216     do
217     {
218         if (*matrixIterator)
219         {
220             listFormats.push_back(gatewayData.listSourceFormats.at((matrixIterator - gatewayData.convertionMatrix.begin()) / gatewayData.listSinkFormats.size()));
221         }
222         std::advance(matrixIterator, gatewayData.listSinkFormats.size());
223     } while (gatewayData.convertionMatrix.end() - matrixIterator > 0);
224 }
225
226 Router::~Router()
227 {
228 }
229
230 RoutingTreeItem::RoutingTreeItem(const am_domainID_t domainID, const am_gatewayID_t gatewayID, RoutingTreeItem *parent) :
231         mDomainID(domainID), //
232         mGatewayID(gatewayID), //
233         mParentItem(parent)
234 {
235     assert(mDomainID!=0);
236 }
237
238 void RoutingTreeItem::appendChild(RoutingTreeItem *newChild)
239 {
240     assert(newChild);
241     mListChildItems.push_back(newChild);
242 }
243
244 void RoutingTreeItem::returnChildItems(std::vector<RoutingTreeItem*> listChildItems)
245 {
246     listChildItems = mListChildItems;
247 }
248
249 am_domainID_t RoutingTreeItem::returnDomainID() const
250 {
251     return (mDomainID);
252 }
253
254 am_gatewayID_t RoutingTreeItem::returnGatewayID() const
255 {
256     return (mGatewayID);
257 }
258
259 RoutingTreeItem* RoutingTreeItem::returnParent() const
260 {
261     return (mParentItem);
262 }
263
264 RoutingTreeItem::~RoutingTreeItem()
265 {
266 }
267
268 RoutingTree::RoutingTree(const am_domainID_t rootDomainID) :
269         mRootItem(RoutingTreeItem(rootDomainID))
270 {
271     assert(rootDomainID!=0);
272 }
273
274 RoutingTreeItem *RoutingTree::insertItem(const am_domainID_t domainID, const am_gatewayID_t gatewayID, RoutingTreeItem *parent)
275 {
276     RoutingTreeItem *newTree = new RoutingTreeItem(domainID, gatewayID, parent);
277     parent->appendChild(newTree);
278     mListChild.push_back(newTree);
279     return newTree;
280 }
281
282 void RoutingTree::getRoute(RoutingTreeItem *targetItem, std::vector<am_gatewayID_t>& listGateways)
283 {
284     listGateways.clear();
285     RoutingTreeItem *parentItem = targetItem;
286     while (parentItem != &mRootItem)
287     {
288         listGateways.push_back(parentItem->returnGatewayID());
289         parentItem = parentItem->returnParent();
290     }
291     std::reverse(listGateways.begin(), listGateways.end());
292 }
293
294 am_domainID_t RoutingTree::returnRootDomainID() const
295 {
296     return (mRootItem.returnDomainID());
297 }
298
299 RoutingTreeItem *RoutingTree::returnRootItem()
300 {
301     return (&mRootItem);
302 }
303
304 RoutingTree::~RoutingTree()
305 {
306     std::vector<RoutingTreeItem*>::iterator it = mListChild.begin();
307     for (; it != mListChild.end(); ++it)
308     {
309         delete *it;
310     }
311 }
312