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, DBUS_PREDEFINED
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 {"dbus_predefined", TypeEnum::DBUS_PREDEFINED}
35 DBusAddressTranslator::DBusAddressTranslator() {}
38 void DBusAddressTranslator::init() {
39 std::string fqnOfConfigFile = getCurrentBinaryFileFQN();
40 std::ifstream addressConfigFile;
42 if (fqnOfConfigFile != "") {
43 fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
45 addressConfigFile.open(fqnOfConfigFile.c_str());
47 if (addressConfigFile.is_open()) {
48 readConfigFile(addressConfigFile);
49 addressConfigFile.close();
52 addressConfigFile.clear();
53 std::vector<std::string> splittedConfigFQN = split(fqnOfConfigFile, '/');
54 std::string globalConfigFQN = DBUS_GLOBAL_CONFIG_ROOT + splittedConfigFQN.at(splittedConfigFQN.size() - 1);
55 addressConfigFile.open(globalConfigFQN);
56 if (addressConfigFile.is_open()) {
57 readConfigFile(addressConfigFile);
58 addressConfigFile.close();
60 addressConfigFile.clear();
63 addressConfigFile.open(DBUS_GLOBAL_CONFIG_FQN);
64 if(addressConfigFile.is_open()) {
65 readConfigFile(addressConfigFile);
66 addressConfigFile.close();
71 inline void readValue(std::string& readLine, CommonApiServiceDetails& serviceDetails) {
72 std::stringstream readStream(readLine);
74 std::string paramName;
75 std::string paramValue;
77 getline(readStream, paramName, '=');
79 auto typeEntry = allowedValueTypes.find(paramName);
80 if(typeEntry != allowedValueTypes.end()) {
81 getline(readStream, paramValue);
82 switch(typeEntry->second) {
83 case TypeEnum::DBUS_CONNECTION:
84 std::get<0>(std::get<0>(serviceDetails)) = paramValue;
86 case TypeEnum::DBUS_OBJECT:
87 std::get<1>(std::get<0>(serviceDetails)) = paramValue;
89 case TypeEnum::DBUS_INTERFACE:
90 std::get<2>(std::get<0>(serviceDetails)) = paramValue;
92 case TypeEnum::DBUS_PREDEFINED:
93 std::get<1>(serviceDetails) = paramValue == "true" ? true : false;
100 inline void reset(DBusServiceAddress& dbusServiceAddress) {
101 std::get<0>(dbusServiceAddress) = "";
102 std::get<1>(dbusServiceAddress) = "";
103 std::get<2>(dbusServiceAddress) = "";
106 inline void reset(CommonApiServiceDetails& serviceDetails) {
107 reset(std::get<0>(serviceDetails));
108 std::get<1>(serviceDetails) = false;
112 void DBusAddressTranslator::readConfigFile(std::ifstream& addressConfigFile) {
113 std::string currentlyParsedCommonApiAddress;
114 CommonApiServiceDetails serviceDetails;
115 reset(serviceDetails);
117 bool newAddressFound = false;
119 while (addressConfigFile.good()) {
120 std::string readLine;
121 getline(addressConfigFile, readLine);
122 const size_t readLineLength = readLine.length();
124 if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
125 if (newAddressFound) {
126 fillUndefinedValues(serviceDetails, currentlyParsedCommonApiAddress);
127 commonApiAddressDetails.insert( {currentlyParsedCommonApiAddress, serviceDetails});
128 dbusToCommonApiAddress.insert( {std::get<0>(serviceDetails), currentlyParsedCommonApiAddress});
130 reset(serviceDetails);
131 currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
132 newAddressFound = commonApiAddressDetails.find(currentlyParsedCommonApiAddress) == commonApiAddressDetails.end();
134 } else if (newAddressFound) {
135 readValue(readLine, serviceDetails);
138 if(newAddressFound) {
139 fillUndefinedValues(serviceDetails, currentlyParsedCommonApiAddress);
140 commonApiAddressDetails.insert( {currentlyParsedCommonApiAddress, serviceDetails});
141 dbusToCommonApiAddress.insert( {std::get<0>(serviceDetails), currentlyParsedCommonApiAddress});
146 void DBusAddressTranslator::fillUndefinedValues(CommonApiServiceDetails& serviceDetails, const std::string& commonApiAddress) const {
147 std::string connectionName;
148 std::string objectPath;
149 std::string interfaceName;
151 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
153 std::get<0>(std::get<0>(serviceDetails)) = std::get<0>(std::get<0>(serviceDetails)) == "" ? connectionName : std::get<0>(std::get<0>(serviceDetails));
154 std::get<1>(std::get<0>(serviceDetails)) = std::get<1>(std::get<0>(serviceDetails)) == "" ? objectPath : std::get<1>(std::get<0>(serviceDetails));
155 std::get<2>(std::get<0>(serviceDetails)) = std::get<2>(std::get<0>(serviceDetails)) == "" ? interfaceName : std::get<2>(std::get<0>(serviceDetails));
159 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
160 static DBusAddressTranslator* dbusNameService_;
161 if(!dbusNameService_) {
162 dbusNameService_ = new DBusAddressTranslator();
163 dbusNameService_->init();
165 return *dbusNameService_;
169 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
170 std::string& interfaceName,
171 std::string& connectionName,
172 std::string& objectPath) {
174 const auto& foundAddressMapping = commonApiAddressDetails.find(commonApiAddress);
176 if(foundAddressMapping != commonApiAddressDetails.end()) {
177 connectionName = std::get<0>(std::get<0>(foundAddressMapping->second));
178 objectPath = std::get<1>(std::get<0>(foundAddressMapping->second));
179 interfaceName = std::get<2>(std::get<0>(foundAddressMapping->second));
181 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
182 commonApiAddressDetails.insert( {commonApiAddress, std::make_tuple(std::make_tuple(connectionName, objectPath, interfaceName), false) } );
186 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
187 const std::string& connectionName,
188 const std::string& objectPath,
189 std::string& commonApiAddress) {
191 DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
193 const auto& foundAddressMapping = dbusToCommonApiAddress.find(dbusAddress);
194 if (foundAddressMapping != dbusToCommonApiAddress.end()) {
195 commonApiAddress = foundAddressMapping->second;
197 findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
198 dbusToCommonApiAddress.insert( {std::move(dbusAddress), commonApiAddress} );
202 void DBusAddressTranslator::getPredefinedInstances(const std::string& connectionName,
203 std::vector<DBusServiceAddress>& instances) {
205 auto dbusAddress = commonApiAddressDetails.begin();
206 while (dbusAddress != commonApiAddressDetails.end()) {
207 CommonApiServiceDetails serviceDetails = dbusAddress->second;
208 if (connectionName == std::get<0>(std::get<0>(serviceDetails))
209 && true == std::get<1>(serviceDetails)) {
210 instances.push_back(std::get<0>(serviceDetails));
216 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
217 std::string& interfaceName,
218 std::string& connectionName,
219 std::string& objectPath) const {
220 std::vector<std::string> parts = split(commonApiAddress, ':');
221 interfaceName = parts[1];
222 connectionName = parts[2];
223 objectPath = '/' + parts[2];
224 std::replace(objectPath.begin(), objectPath.end(), '.', '/');
227 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
228 const std::string& interfaceName,
229 const std::string& connectionName,
230 const std::string& objectPath) const {
231 commonApiAddress = "local:" + interfaceName + ":" + connectionName;
236 }// namespace CommonAPI