Successful parsing of config file
[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 #include "DBusUtils.h"
11
12 #include <unordered_set>
13 #include <string.h>
14 #include <iostream>
15 #include <fstream>
16
17
18 namespace CommonAPI {
19 namespace DBus {
20
21
22
23
24
25 inline bool readSection(std::ifstream& addressConfigFile, std::string& section) {
26     std::string readLine;
27     getline(addressConfigFile, readLine);
28
29     const size_t sectionLength = readLine.length();
30
31     if(readLine[0] != '[' || readLine[sectionLength - 1] != ']') {
32         return false;
33     }
34
35     section = readLine.substr(1, sectionLength - 2);
36
37     return true;
38
39 //    [domain:service:instance]
40 //    dbus_connection=connection.name
41 //    dbus_object=/path/to/object
42 //    dbus_interface=service.name
43 }
44
45
46 std::unordered_set<std::string> allowedValueTypes = {"dbus_connection", "dbus_object", "dbus_interface"};
47
48
49 inline bool readValue(std::ifstream& addressConfigFile, std::string& paramName, std::string& paramValue) {
50     getline(addressConfigFile, paramName, '=');
51     if(allowedValueTypes.find(paramName) == allowedValueTypes.end()) {
52         return false;
53     }
54     getline(addressConfigFile, paramValue);
55
56     //TODO: Check whether its a valid bus name
57
58     return true;
59 }
60
61
62
63
64
65
66 DBusAddressTranslator::DBusAddressTranslator()
67 {
68     std::string fqnOfConfigFile = getBinaryFileName();
69     fqnOfConfigFile += "_dbus.ini";
70
71     std::ifstream addressConfigFile;
72
73     addressConfigFile.open(fqnOfConfigFile.c_str());
74
75     while (addressConfigFile.good()) {
76         std::string section;
77
78         readSection(addressConfigFile, section);
79         std::cout << "---" << section<< "---" << std::endl;
80
81         std::string paramName;
82         std::string paramValue;
83
84         readValue(addressConfigFile, paramName, paramValue);
85         std::cout << paramName << "::" << paramValue << std::endl;
86         readValue(addressConfigFile, paramName, paramValue);
87         std::cout << paramName << "::" << paramValue << std::endl;
88         readValue(addressConfigFile, paramName, paramValue);
89         std::cout << paramName << "::" << paramValue << std::endl;
90     }
91
92     addressConfigFile.close();
93 }
94
95
96 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
97     static DBusAddressTranslator* dbusNameService_;
98     if(!dbusNameService_) {
99         dbusNameService_ = new DBusAddressTranslator();
100     }
101     return *dbusNameService_;
102 }
103
104
105 void DBusAddressTranslator::searchForDBusInstanceId(const std::string& instanceId,
106                                               std::string& connectionName,
107                                               std::string& objectPath) const {
108     if(!true) {
109         findFallbackDBusInstanceId(instanceId, connectionName, objectPath);
110     }
111 }
112
113 void DBusAddressTranslator::searchForCommonInstanceId(std::string& instanceId,
114                                                 const std::string& connectionName,
115                                                 const std::string& objectPath) const {
116     if(!true) {
117         findFallbackCommonInstanceId(instanceId, connectionName, objectPath);
118     }
119 }
120
121
122 std::string DBusAddressTranslator::findCommonAPIAddressForDBusAddress(const std::string& conName,
123                                                const std::string& objName,
124                                                const std::string& intName) const {
125     return "local:" + intName + ":" + conName;
126 }
127
128 void DBusAddressTranslator::findFallbackDBusInstanceId(const std::string& instanceId,
129                                                  std::string& connectionName,
130                                                  std::string& objectPath) const {
131     connectionName = instanceId;
132     objectPath = '/' + instanceId;
133     std::replace(objectPath.begin(), objectPath.end(), '.', '/');
134 }
135
136 void DBusAddressTranslator::findFallbackCommonInstanceId(std::string& instanceId,
137                                                    const std::string& connectionName,
138                                                    const std::string& objectPath) const {
139     instanceId = connectionName;
140 }
141
142
143 }// namespace DBus
144 }// namespace CommonAPI