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"
21 DBusAddressTranslator::DBusAddressTranslator() {
22 char fqnOfBinary[FILENAME_MAX];
23 char pathToProcessImage[FILENAME_MAX];
25 sprintf(pathToProcessImage, "/proc/%ld/exe", getpid());
26 unsigned int lengthOfFqn;
27 lengthOfFqn = readlink(pathToProcessImage, fqnOfBinary, sizeof(fqnOfBinary) - 1);
29 if (lengthOfFqn != -1) {
30 fqnOfBinary[lengthOfFqn] = '\0';
31 std::string fqnOfConfigFile(std::move(fqnOfBinary));
32 fqnOfConfigFile += "_dbus.ini";
34 std::ifstream addressConfigFile;
36 addressConfigFile.open(fqnOfConfigFile.c_str());
39 while (addressConfigFile.good()) {
40 std::getline(addressConfigFile, readLine);
41 std::cout << readLine << std::endl;
44 addressConfigFile.close();
49 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
50 static DBusAddressTranslator* dbusNameService_;
51 if(!dbusNameService_) {
52 dbusNameService_ = new DBusAddressTranslator();
54 return *dbusNameService_;
58 void DBusAddressTranslator::searchForDBusInstanceId(const std::string& instanceId,
59 std::string& connectionName,
60 std::string& objectPath) const {
62 findFallbackDBusInstanceId(instanceId, connectionName, objectPath);
66 void DBusAddressTranslator::searchForCommonInstanceId(std::string& instanceId,
67 const std::string& connectionName,
68 const std::string& objectPath) const {
70 findFallbackCommonInstanceId(instanceId, connectionName, objectPath);
75 std::string DBusAddressTranslator::findCommonAPIAddressForDBusAddress(const std::string& conName,
76 const std::string& objName,
77 const std::string& intName) const {
78 return "local:" + intName + ":" + conName;
81 void DBusAddressTranslator::findFallbackDBusInstanceId(const std::string& instanceId,
82 std::string& connectionName,
83 std::string& objectPath) const {
84 connectionName = instanceId;
85 objectPath = '/' + instanceId;
86 std::replace(objectPath.begin(), objectPath.end(), '.', '/');
89 void DBusAddressTranslator::findFallbackCommonInstanceId(std::string& instanceId,
90 const std::string& connectionName,
91 const std::string& objectPath) const {
92 instanceId = connectionName;
97 }// namespace CommonAPI