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/. */
9 #include "DBusAddressTranslator.h"
10 #include "DBusUtils.h"
12 #include <unordered_set>
24 DBUS_CONNECTION, DBUS_OBJECT, DBUS_INTERFACE
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}
34 inline void readValue(std::string& readLine, DBusServiceAddress& dbusServiceAddress) {
35 std::stringstream readStream(readLine);
37 std::string paramName;
38 std::string paramValue;
40 getline(readStream, paramName, '=');
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;
49 case TypeEnum::DBUS_OBJECT:
50 std::get<1>(dbusServiceAddress) = paramValue;
52 case TypeEnum::DBUS_INTERFACE:
53 std::get<2>(dbusServiceAddress) = paramValue;
60 inline void reset(DBusServiceAddress& dbusServiceAddress) {
61 std::get<0>(dbusServiceAddress) = "";
62 std::get<1>(dbusServiceAddress) = "";
63 std::get<2>(dbusServiceAddress) = "";
67 void DBusAddressTranslator::fillUndefinedValues(DBusServiceAddress& dbusServiceAddress, const std::string& commonApiAddress) const {
68 std::string connectionName;
69 std::string objectPath;
70 std::string interfaceName;
72 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
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);
80 DBusAddressTranslator::DBusAddressTranslator() {}
83 void DBusAddressTranslator::init() {
84 std::string fqnOfConfigFile = getCurrentBinaryFileFQN();
85 fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
87 std::ifstream addressConfigFile;
88 addressConfigFile.open(fqnOfConfigFile.c_str());
90 if(addressConfigFile.is_open()) {
91 readConfigFile(addressConfigFile);
93 addressConfigFile.clear();
94 addressConfigFile.open(DBUS_GLOBAL_CONFIG_FQN);
95 if(addressConfigFile.is_open()) {
96 readConfigFile(addressConfigFile);
102 void DBusAddressTranslator::readConfigFile(std::ifstream& addressConfigFile) {
103 std::string currentlyParsedCommonApiAddress;
104 DBusServiceAddress dbusServiceAddress;
105 reset(dbusServiceAddress);
107 bool currentAddressNotYetContained = false;
108 bool atLeastOneAddressFound = false;
110 while (addressConfigFile.good()) {
111 std::string readLine;
112 getline(addressConfigFile, readLine);
113 const size_t readLineLength = readLine.length();
115 if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
116 if (atLeastOneAddressFound) {
117 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
118 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
119 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
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;
128 } else if (currentAddressNotYetContained) {
129 readValue(readLine, dbusServiceAddress);
132 if(atLeastOneAddressFound) {
133 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
134 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
135 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
138 addressConfigFile.close();
142 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
143 static DBusAddressTranslator* dbusNameService_;
144 if(!dbusNameService_) {
145 dbusNameService_ = new DBusAddressTranslator();
146 dbusNameService_->init();
148 return *dbusNameService_;
152 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
153 std::string& interfaceName,
154 std::string& connectionName,
155 std::string& objectPath) {
157 const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
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);
164 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
165 knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
169 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
170 const std::string& connectionName,
171 const std::string& objectPath,
172 std::string& commonApiAddress) {
174 DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
176 const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
177 if (foundAddressMapping != knownCommonAddresses.end()) {
178 commonApiAddress = foundAddressMapping->second;
180 findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
181 knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
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(), '.', '/');
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;
206 }// namespace CommonAPI