swift
Loading...
Searching...
No Matches
config.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "config.h"
5
6#include <algorithm>
7#include <cctype>
8#include <chrono>
9#include <fstream>
10#include <iomanip>
11#include <string>
12
13#include "utils.h"
14
16
17using namespace swift::misc::simulation::xplane::qtfreeutils;
18
19namespace XSwiftBus
20{
22
24
26 {
27 std::ifstream configFile(m_filePath);
28 if (!configFile.is_open())
29 {
30 return;
31 }
32
33 std::string line;
34 for (int lineNo = 1; std::getline(configFile, line); ++lineNo)
35 {
36 line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
37 if (line.empty() || line[0] == '#')
38 {
39 continue;
40 }
41
42 auto delimiterPos = line.find("=");
43 if (delimiterPos == std::string::npos)
44 {
45 WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
46 continue;
47 }
48
49 std::string key = line.substr(0, delimiterPos);
50 std::string value = line.substr(delimiterPos + 1);
51
52 if (key.empty() || value.empty())
53 {
54 WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
55 continue;
56 }
57
58 bool valid = true;
59 if (stringCompareCaseInsensitive(key, "dbusMode"))
60 {
61 valid = parseDBusMode(value);
62 }
63 else if (stringCompareCaseInsensitive(key, "dbusAddress"))
64 {
65 valid = parseDBusAddress(value);
66 }
67 else if (stringCompareCaseInsensitive(key, "dbusPort"))
68 {
69 valid = parseDBusPort(value);
70 }
71 else if (stringCompareCaseInsensitive(key, "debug"))
72 {
73 valid = parseDebug(value);
74 }
75 else if (stringCompareCaseInsensitive(key, "tcas"))
76 {
77 valid = parseTcas(value);
78 }
79 else
80 {
81 WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Unknown variable " + key + "!");
82 continue;
83 }
84
85 if (!valid)
86 {
87 WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
88 continue;
89 }
90 }
91 }
92
94 {
95 DEBUG_LOG("xswiftbus configuration:");
96 DEBUG_LOG("DBus mode: " + dbusModeToString(m_dbusMode));
97 DEBUG_LOG("DBus server address: " + m_dbusAddress);
98 DEBUG_LOG("DBus server port: " + std::to_string(m_dbusPort));
99 }
100
101 bool CConfig::writeConfig(bool tcas, bool debug)
102 {
103 setTcasEnabled(tcas);
104 setDebugMode(debug);
105 return writeConfigFile();
106 }
107
108 bool CConfig::writeConfigFile() const
109 {
110 std::ofstream configFile(m_filePath, std::ofstream::out | std::ofstream::trunc);
111 if (!configFile.is_open())
112 {
113 return false;
114 }
115
116 // this code should be similar to CXSwiftBusConfigWriter
117 configFile << "# DBus Mode - Options: p2p, session" << std::endl;
118 configFile << "dbusMode = " << toLower(dbusModeToString(m_dbusMode)) << std::endl;
119 configFile << std::endl;
120 configFile << "# DBus server address - relevant for P2P mode only" << std::endl;
121 configFile << "dbusAddress = " << m_dbusAddress << std::endl;
122 configFile << std::endl;
123 configFile << "# DBus server port - relevant for P2P mode only" << std::endl;
124 configFile << "dbusPort = " << m_dbusPort << std::endl;
125 configFile << std::endl;
126 configFile << "# Render phase debugging - to help diagnose crashes" << std::endl;
127 configFile << "debug = " << boolToOnOff(m_debug) << std::endl;
128 configFile << std::endl;
129 configFile << "# TCAS traffic - to disable in case of crashes" << std::endl;
130 configFile << "tcas = " << boolToOnOff(m_tcas) << std::endl;
131
132 // for info
133 const auto clockNow = std::chrono::system_clock::now();
134 const time_t now = std::chrono::system_clock::to_time_t(clockNow);
135 struct tm tms {};
136#if defined(IBM)
137 localtime_s(&tms, &now);
138#else
139 localtime_r(&now, &tms);
140#endif
141 configFile << std::endl;
142 configFile << "# Updated by xswiftbus plugin " << std::put_time(&tms, "%T");
143 configFile << std::endl;
144 configFile.close();
145 return true;
146 }
147
148 bool CConfig::parseDBusMode(const std::string &value)
149 {
150 if (stringCompareCaseInsensitive(value, "session"))
151 {
152 m_dbusMode = CConfig::DBusSession;
153 return true;
154 }
155 else if (stringCompareCaseInsensitive(value, "P2P"))
156 {
157 m_dbusMode = CConfig::DBusP2P;
158 return true;
159 }
160 else
161 {
162 return false;
163 }
164 }
165
166 bool CConfig::parseDBusAddress(const std::string &value)
167 {
168 m_dbusAddress = value;
169 return true;
170 }
171
172 bool CConfig::parseDBusPort(const std::string &value)
173 {
174 int port = 0;
175 try
176 {
177 port = std::stoi(value);
178 }
179 catch (...)
180 {
181 return false;
182 }
183
184 if (port < 0 || port > 65535)
185 {
186 return false;
187 }
188 m_dbusPort = port;
189 return true;
190 }
191
192 bool CConfig::parseDebug(const std::string &value)
193 {
194 if (stringCompareCaseInsensitive(value, "on"))
195 {
196 m_debug = true;
197 }
198 else
199 {
200 m_debug = false;
201 }
202 return true;
203 }
204
205 bool CConfig::parseTcas(const std::string &value)
206 {
207 m_tcas = stringCompareCaseInsensitive(value, "on");
208 return true;
209 }
210
211 std::string CConfig::dbusModeToString(DBusMode mode)
212 {
213 switch (mode)
214 {
215 case DBusSession: return "Session";
216 case DBusP2P: return "P2P";
217 }
218 return {};
219 }
220
221 std::string CConfig::boolToOnOff(bool on)
222 {
223 if (on)
224 {
225 return "on";
226 }
227 return "off";
228 }
229} // namespace XSwiftBus
CConfig()
Constructor.
Definition config.cpp:21
void setTcasEnabled(bool on)
Set TCAS traffic on/off.
Definition config.h:58
void print()
Print the current configuration to the X-Plane log.
Definition config.cpp:93
bool writeConfig(bool tcas, bool debug)
Update and write config file.
Definition config.cpp:101
~CConfig()
Destructor;.
Definition config.cpp:23
void parse()
Load and parse config file.
Definition config.cpp:25
void setDebugMode(bool on)
Set debug mode.
Definition config.h:52
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14
SWIFT_MISC_EXPORT const QString & boolToOnOff(bool v)
Bool to on/off.
bool stringCompareCaseInsensitive(const std::string &str1, const std::string &str2)
Compare case insensitive.
std::string toLower(std::string s)
String to lower case.
#define DEBUG_LOG(msg)
Logger convenience macros.
Definition utils.h:46
#define WARNING_LOG(msg)
Logger convenience macros.
Definition utils.h:50