Successfully read in local .ini file w/o relying on commandline
[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
11 #include <iostream>
12 #include <fstream>
13 #include <unistd.h>
14 #include <string.h>
15
16
17 namespace CommonAPI {
18 namespace DBus {
19
20
21 DBusAddressTranslator::DBusAddressTranslator() {
22     char fqnOfBinary[FILENAME_MAX];
23     char pathToProcessImage[FILENAME_MAX];
24
25     sprintf(pathToProcessImage, "/proc/%ld/exe", getpid());
26     unsigned int lengthOfFqn;
27     lengthOfFqn = readlink(pathToProcessImage, fqnOfBinary, sizeof(fqnOfBinary) - 1);
28
29     if (lengthOfFqn != -1) {
30         fqnOfBinary[lengthOfFqn] = '\0';
31         std::string fqnOfConfigFile(std::move(fqnOfBinary));
32         fqnOfConfigFile += "_dbus.ini";
33
34         std::ifstream addressConfigFile;
35
36         addressConfigFile.open(fqnOfConfigFile.c_str());
37
38         std::string readLine;
39         while (addressConfigFile.good()) {
40             std::getline(addressConfigFile, readLine);
41             std::cout << readLine << std::endl;
42         }
43
44         addressConfigFile.close();
45     }
46 }
47
48
49 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
50     static DBusAddressTranslator* dbusNameService_;
51     if(!dbusNameService_) {
52         dbusNameService_ = new DBusAddressTranslator();
53     }
54     return *dbusNameService_;
55 }
56
57
58 void DBusAddressTranslator::searchForDBusInstanceId(const std::string& instanceId,
59                                               std::string& connectionName,
60                                               std::string& objectPath) const {
61     if(!true) {
62         findFallbackDBusInstanceId(instanceId, connectionName, objectPath);
63     }
64 }
65
66 void DBusAddressTranslator::searchForCommonInstanceId(std::string& instanceId,
67                                                 const std::string& connectionName,
68                                                 const std::string& objectPath) const {
69     if(!true) {
70         findFallbackCommonInstanceId(instanceId, connectionName, objectPath);
71     }
72 }
73
74
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;
79 }
80
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(), '.', '/');
87 }
88
89 void DBusAddressTranslator::findFallbackCommonInstanceId(std::string& instanceId,
90                                                    const std::string& connectionName,
91                                                    const std::string& objectPath) const {
92     instanceId = connectionName;
93 }
94
95
96 }// namespace DBus
97 }// namespace CommonAPI