Optional global address config files in
[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         std::vector<std::string> splittedConfigFQN = split(fqnOfConfigFile, '/');
95         std::string globalConfigFQN = DBUS_GLOBAL_CONFIG_ROOT + splittedConfigFQN.at(splittedConfigFQN.size() - 1);
96         addressConfigFile.open(globalConfigFQN);
97         if(addressConfigFile.is_open()) {
98             readConfigFile(addressConfigFile);
99         }
100     }
101 }
102
103
104 void DBusAddressTranslator::readConfigFile(std::ifstream& addressConfigFile) {
105     std::string currentlyParsedCommonApiAddress;
106     DBusServiceAddress dbusServiceAddress;
107     reset(dbusServiceAddress);
108
109     bool currentAddressNotYetContained = false;
110     bool atLeastOneAddressFound = false;
111
112     while (addressConfigFile.good()) {
113         std::string readLine;
114         getline(addressConfigFile, readLine);
115         const size_t readLineLength = readLine.length();
116
117         if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
118             if (atLeastOneAddressFound) {
119                 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
120                 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
121                 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
122             }
123             reset(dbusServiceAddress);
124             currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
125             currentAddressNotYetContained =
126                             knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end() &&
127                             knownCommonAddresses.find(dbusServiceAddress) == knownCommonAddresses.end();
128             atLeastOneAddressFound = true;
129
130         } else if (currentAddressNotYetContained) {
131             readValue(readLine, dbusServiceAddress);
132         }
133     }
134     if(atLeastOneAddressFound) {
135         fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
136         knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
137         knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
138     }
139
140     addressConfigFile.close();
141 }
142
143
144 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
145     static DBusAddressTranslator* dbusNameService_;
146     if(!dbusNameService_) {
147         dbusNameService_ = new DBusAddressTranslator();
148         dbusNameService_->init();
149     }
150     return *dbusNameService_;
151 }
152
153
154 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
155                                                  std::string& interfaceName,
156                                                  std::string& connectionName,
157                                                  std::string& objectPath) {
158
159     const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
160
161     if(foundAddressMapping != knownDBusAddresses.end()) {
162         connectionName = std::get<0>(foundAddressMapping->second);
163         objectPath = std::get<1>(foundAddressMapping->second);
164         interfaceName = std::get<2>(foundAddressMapping->second);
165     } else {
166         findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
167         knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
168     }
169 }
170
171 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
172                                                    const std::string& connectionName,
173                                                    const std::string& objectPath,
174                                                    std::string& commonApiAddress) {
175
176     DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
177
178     const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
179     if (foundAddressMapping != knownCommonAddresses.end()) {
180         commonApiAddress = foundAddressMapping->second;
181     } else {
182         findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
183         knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
184     }
185 }
186
187
188 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
189                                                     std::string& interfaceName,
190                                                     std::string& connectionName,
191                                                     std::string& objectPath) const {
192     std::vector<std::string> parts = split(commonApiAddress, ':');
193     interfaceName = parts[1];
194     connectionName = parts[2];
195     objectPath = '/' + parts[2];
196     std::replace(objectPath.begin(), objectPath.end(), '.', '/');
197 }
198
199 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
200                                                       const std::string& interfaceName,
201                                                       const std::string& connectionName,
202                                                       const std::string& objectPath) const {
203     commonApiAddress = "local:" + interfaceName + ":" + connectionName;
204 }
205
206
207 }// namespace DBus
208 }// namespace CommonAPI