Changed global dbus address config file to fixed name
[profile/ivi/common-api-dbus-runtime.git] / src / CommonAPI / DBus / DBusAddressTranslator.cpp
1 /* Copyright (C) 2013 BMW Group
2  * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3  * Author: Juergen Gehring (juergen.gehring@bmw.de)
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8
9 #include "DBusAddressTranslator.h"
10 #include "DBusUtils.h"
11
12 #include <unordered_set>
13 #include <string.h>
14 #include <iostream>
15 #include <fstream>
16 #include <cassert>
17
18
19 namespace CommonAPI {
20 namespace DBus {
21
22
23 enum class TypeEnum {
24     DBUS_CONNECTION, DBUS_OBJECT, DBUS_INTERFACE
25 };
26
27 static const std::unordered_map<std::string, TypeEnum> allowedValueTypes = {
28                 {"dbus_connection", TypeEnum::DBUS_CONNECTION},
29                 {"dbus_object", TypeEnum::DBUS_OBJECT},
30                 {"dbus_interface", TypeEnum::DBUS_INTERFACE}
31 };
32
33
34 inline void readValue(std::string& readLine, DBusServiceAddress& dbusServiceAddress) {
35     std::stringstream readStream(readLine);
36
37     std::string paramName;
38     std::string paramValue;
39
40     getline(readStream, paramName, '=');
41
42     auto typeEntry = allowedValueTypes.find(paramName);
43     if(typeEntry != allowedValueTypes.end()) {
44         getline(readStream, paramValue);
45         switch(typeEntry->second) {
46             case TypeEnum::DBUS_CONNECTION:
47                 std::get<0>(dbusServiceAddress) = paramValue;
48                 break;
49             case TypeEnum::DBUS_OBJECT:
50                 std::get<1>(dbusServiceAddress) = paramValue;
51                 break;
52             case TypeEnum::DBUS_INTERFACE:
53                 std::get<2>(dbusServiceAddress) = paramValue;
54                 break;
55         }
56     }
57 }
58
59
60 inline void reset(DBusServiceAddress& dbusServiceAddress) {
61     std::get<0>(dbusServiceAddress) = "";
62     std::get<1>(dbusServiceAddress) = "";
63     std::get<2>(dbusServiceAddress) = "";
64 }
65
66
67 void DBusAddressTranslator::fillUndefinedValues(DBusServiceAddress& dbusServiceAddress, const std::string& commonApiAddress) const {
68     std::string connectionName;
69     std::string objectPath;
70     std::string interfaceName;
71
72     findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
73
74     std::get<0>(dbusServiceAddress) = std::get<0>(dbusServiceAddress) == "" ? connectionName : std::get<0>(dbusServiceAddress);
75     std::get<1>(dbusServiceAddress) = std::get<1>(dbusServiceAddress) == "" ? objectPath : std::get<1>(dbusServiceAddress);
76     std::get<2>(dbusServiceAddress) = std::get<2>(dbusServiceAddress) == "" ? interfaceName : std::get<2>(dbusServiceAddress);
77 }
78
79
80 DBusAddressTranslator::DBusAddressTranslator() {}
81
82
83 void DBusAddressTranslator::init() {
84     std::string fqnOfConfigFile = getCurrentBinaryFileFQN();
85     fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
86
87     std::ifstream addressConfigFile;
88     addressConfigFile.open(fqnOfConfigFile.c_str());
89
90     if(addressConfigFile.is_open()) {
91         readConfigFile(addressConfigFile);
92     } else {
93         addressConfigFile.clear();
94         addressConfigFile.open(DBUS_GLOBAL_CONFIG_FQN);
95         if(addressConfigFile.is_open()) {
96             readConfigFile(addressConfigFile);
97         }
98     }
99 }
100
101
102 void DBusAddressTranslator::readConfigFile(std::ifstream& addressConfigFile) {
103     std::string currentlyParsedCommonApiAddress;
104     DBusServiceAddress dbusServiceAddress;
105     reset(dbusServiceAddress);
106
107     bool currentAddressNotYetContained = false;
108     bool atLeastOneAddressFound = false;
109
110     while (addressConfigFile.good()) {
111         std::string readLine;
112         getline(addressConfigFile, readLine);
113         const size_t readLineLength = readLine.length();
114
115         if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
116             if (atLeastOneAddressFound) {
117                 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
118                 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
119                 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
120             }
121             reset(dbusServiceAddress);
122             currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
123             currentAddressNotYetContained =
124                             knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end() &&
125                             knownCommonAddresses.find(dbusServiceAddress) == knownCommonAddresses.end();
126             atLeastOneAddressFound = true;
127
128         } else if (currentAddressNotYetContained) {
129             readValue(readLine, dbusServiceAddress);
130         }
131     }
132     if(atLeastOneAddressFound) {
133         fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
134         knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
135         knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
136     }
137
138     addressConfigFile.close();
139 }
140
141
142 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
143     static DBusAddressTranslator* dbusNameService_;
144     if(!dbusNameService_) {
145         dbusNameService_ = new DBusAddressTranslator();
146         dbusNameService_->init();
147     }
148     return *dbusNameService_;
149 }
150
151
152 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
153                                                  std::string& interfaceName,
154                                                  std::string& connectionName,
155                                                  std::string& objectPath) {
156
157     const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
158
159     if(foundAddressMapping != knownDBusAddresses.end()) {
160         connectionName = std::get<0>(foundAddressMapping->second);
161         objectPath = std::get<1>(foundAddressMapping->second);
162         interfaceName = std::get<2>(foundAddressMapping->second);
163     } else {
164         findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
165         knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
166     }
167 }
168
169 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
170                                                    const std::string& connectionName,
171                                                    const std::string& objectPath,
172                                                    std::string& commonApiAddress) {
173
174     DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
175
176     const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
177     if (foundAddressMapping != knownCommonAddresses.end()) {
178         commonApiAddress = foundAddressMapping->second;
179     } else {
180         findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
181         knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
182     }
183 }
184
185
186 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
187                                                     std::string& interfaceName,
188                                                     std::string& connectionName,
189                                                     std::string& objectPath) const {
190     std::vector<std::string> parts = split(commonApiAddress, ':');
191     interfaceName = parts[1];
192     connectionName = parts[2];
193     objectPath = '/' + parts[2];
194     std::replace(objectPath.begin(), objectPath.end(), '.', '/');
195 }
196
197 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
198                                                       const std::string& interfaceName,
199                                                       const std::string& connectionName,
200                                                       const std::string& objectPath) const {
201     commonApiAddress = "local:" + interfaceName + ":" + connectionName;
202 }
203
204
205 }// namespace DBus
206 }// namespace CommonAPI