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 = getCurrentBinaryFileName();
85 fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
87 std::ifstream addressConfigFile;
89 addressConfigFile.open(fqnOfConfigFile.c_str());
91 if(addressConfigFile) {
92 std::string currentlyParsedCommonApiAddress;
93 DBusServiceAddress dbusServiceAddress;
94 reset(dbusServiceAddress);
96 bool currentAddressNotYetContained = false;
97 bool atLeastOneAddressFound = false;
99 while (addressConfigFile.good()) {
100 std::string readLine;
101 getline(addressConfigFile, readLine);
102 const size_t readLineLength = readLine.length();
104 if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
105 if (atLeastOneAddressFound) {
106 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
107 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
108 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
110 reset(dbusServiceAddress);
111 currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
112 currentAddressNotYetContained =
113 knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end() &&
114 knownCommonAddresses.find(dbusServiceAddress) == knownCommonAddresses.end();
115 atLeastOneAddressFound = true;
117 } else if (currentAddressNotYetContained) {
118 readValue(readLine, dbusServiceAddress);
121 if(atLeastOneAddressFound) {
122 fillUndefinedValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
123 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
124 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
127 addressConfigFile.close();
132 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
133 static DBusAddressTranslator* dbusNameService_;
134 if(!dbusNameService_) {
135 dbusNameService_ = new DBusAddressTranslator();
136 dbusNameService_->init();
138 return *dbusNameService_;
142 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
143 std::string& interfaceName,
144 std::string& connectionName,
145 std::string& objectPath) {
147 const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
148 if(foundAddressMapping != knownDBusAddresses.end()) {
149 connectionName = std::get<0>(foundAddressMapping->second);
150 objectPath = std::get<1>(foundAddressMapping->second);
151 interfaceName = std::get<2>(foundAddressMapping->second);
153 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
154 knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
158 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
159 const std::string& connectionName,
160 const std::string& objectPath,
161 std::string& commonApiAddress) {
163 DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
165 const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
166 if (foundAddressMapping != knownCommonAddresses.end()) {
167 commonApiAddress = foundAddressMapping->second;
169 findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
170 knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
175 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
176 std::string& interfaceName,
177 std::string& connectionName,
178 std::string& objectPath) const {
179 std::vector<std::string> parts = split(commonApiAddress, ':');
180 interfaceName = parts[1];
181 connectionName = parts[2];
182 objectPath = '/' + parts[2];
183 std::replace(objectPath.begin(), objectPath.end(), '.', '/');
186 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
187 const std::string& interfaceName,
188 const std::string& connectionName,
189 const std::string& objectPath) const {
190 commonApiAddress = "local:" + interfaceName + ":" + connectionName;
195 }// namespace CommonAPI