swift
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 
17 using namespace swift::misc::simulation::xplane::qtfreeutils;
18 
19 namespace XSwiftBus
20 {
21  CConfig::CConfig() {}
22 
23  CConfig::~CConfig() {}
24 
25  void CConfig::parse()
26  {
27  std::ifstream configFile(m_filePath);
28  if (!configFile.is_open()) { return; }
29 
30  std::string line;
31  for (int lineNo = 1; std::getline(configFile, line); ++lineNo)
32  {
33  line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
34  if (line.empty() || line[0] == '#') { continue; }
35 
36  auto delimiterPos = line.find("=");
37  if (delimiterPos == std::string::npos)
38  {
39  WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
40  continue;
41  }
42 
43  std::string key = line.substr(0, delimiterPos);
44  std::string value = line.substr(delimiterPos + 1);
45 
46  if (key.empty() || value.empty())
47  {
48  WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
49  continue;
50  }
51 
52  bool valid = true;
53  if (stringCompareCaseInsensitive(key, "dbusMode")) { valid = parseDBusMode(value); }
54  else if (stringCompareCaseInsensitive(key, "dbusAddress")) { valid = parseDBusAddress(value); }
55  else if (stringCompareCaseInsensitive(key, "dbusPort")) { valid = parseDBusPort(value); }
56  else if (stringCompareCaseInsensitive(key, "debug")) { valid = parseDebug(value); }
57  else if (stringCompareCaseInsensitive(key, "tcas")) { valid = parseTcas(value); }
58  else
59  {
60  WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Unknown variable " + key + "!");
61  continue;
62  }
63 
64  if (!valid)
65  {
66  WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!");
67  continue;
68  }
69  }
70  }
71 
72  void CConfig::print()
73  {
74  DEBUG_LOG("xswiftbus configuration:");
75  DEBUG_LOG("DBus mode: " + dbusModeToString(m_dbusMode));
76  DEBUG_LOG("DBus server address: " + m_dbusAddress);
77  DEBUG_LOG("DBus server port: " + std::to_string(m_dbusPort));
78  }
79 
80  bool CConfig::writeConfig(bool tcas, bool debug)
81  {
82  setTcasEnabled(tcas);
83  setDebugMode(debug);
84  return writeConfigFile();
85  }
86 
87  bool CConfig::writeConfigFile() const
88  {
89  std::ofstream configFile(m_filePath, std::ofstream::out | std::ofstream::trunc);
90  if (!configFile.is_open()) { return false; }
91 
92  // this code should be similar to CXSwiftBusConfigWriter
93  configFile << "# DBus Mode - Options: p2p, session" << std::endl;
94  configFile << "dbusMode = " << toLower(dbusModeToString(m_dbusMode)) << std::endl;
95  configFile << std::endl;
96  configFile << "# DBus server address - relevant for P2P mode only" << std::endl;
97  configFile << "dbusAddress = " << m_dbusAddress << std::endl;
98  configFile << std::endl;
99  configFile << "# DBus server port - relevant for P2P mode only" << std::endl;
100  configFile << "dbusPort = " << m_dbusPort << std::endl;
101  configFile << std::endl;
102  configFile << "# Render phase debugging - to help diagnose crashes" << std::endl;
103  configFile << "debug = " << boolToOnOff(m_debug) << std::endl;
104  configFile << std::endl;
105  configFile << "# TCAS traffic - to disable in case of crashes" << std::endl;
106  configFile << "tcas = " << boolToOnOff(m_tcas) << std::endl;
107 
108  // for info
109  const auto clockNow = std::chrono::system_clock::now();
110  const time_t now = std::chrono::system_clock::to_time_t(clockNow);
111  struct tm tms;
112 #if defined(IBM)
113  localtime_s(&tms, &now);
114 #else
115  localtime_r(&now, &tms);
116 #endif
117  configFile << std::endl;
118  configFile << "# Updated by xswiftbus plugin " << std::put_time(&tms, "%T");
119  configFile << std::endl;
120  configFile.close();
121  return true;
122  }
123 
124  bool CConfig::parseDBusMode(const std::string &value)
125  {
126  if (stringCompareCaseInsensitive(value, "session"))
127  {
128  m_dbusMode = CConfig::DBusSession;
129  return true;
130  }
131  else if (stringCompareCaseInsensitive(value, "P2P"))
132  {
133  m_dbusMode = CConfig::DBusP2P;
134  return true;
135  }
136  else { return false; }
137  }
138 
139  bool CConfig::parseDBusAddress(const std::string &value)
140  {
141  m_dbusAddress = value;
142  return true;
143  }
144 
145  bool CConfig::parseDBusPort(const std::string &value)
146  {
147  int port = 0;
148  try
149  {
150  port = std::stoi(value);
151  }
152  catch (...)
153  {
154  return false;
155  }
156 
157  if (port < 0 || port > 65535) { return false; }
158  m_dbusPort = port;
159  return true;
160  }
161 
162  bool CConfig::parseDebug(const std::string &value)
163  {
164  if (stringCompareCaseInsensitive(value, "on")) { m_debug = true; }
165  else { m_debug = false; }
166  return true;
167  }
168 
169  bool CConfig::parseTcas(const std::string &value)
170  {
171  m_tcas = stringCompareCaseInsensitive(value, "on");
172  return true;
173  }
174 
175  std::string CConfig::dbusModeToString(DBusMode mode)
176  {
177  switch (mode)
178  {
179  case DBusSession: return "Session";
180  case DBusP2P: return "P2P";
181  }
182  return {};
183  }
184 
185  std::string CConfig::boolToOnOff(bool on)
186  {
187  if (on) { return "on"; }
188  return "off";
189  }
190 } // namespace XSwiftBus
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.
Definition: qtfreeutils.h:120
std::string toLower(std::string s)
String to lower case.
Definition: qtfreeutils.h:113
#define DEBUG_LOG(msg)
Logger convenience macros.
Definition: utils.h:47
#define WARNING_LOG(msg)
Logger convenience macros.
Definition: utils.h:51