swift
service.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_SIM_XSWIFTBUS_SERVICE_H
5 #define SWIFT_SIM_XSWIFTBUS_SERVICE_H
6 
8 
9 #ifndef NOMINMAX
10 # define NOMINMAX
11 #endif
12 
13 // clang-format off
14 #include "dbusobject.h"
15 #include "datarefs.h"
16 #include "messages.h"
17 #include "terrainprobe.h"
18 #include <XPLM/XPLMNavigation.h>
19 #include <string>
20 #include <chrono>
21 #include <limits>
22 // clang-format on
23 
25 #define XSWIFTBUS_SERVICE_INTERFACENAME "org.swift_project.xswiftbus.service"
26 #define XSWIFTBUS_SERVICE_OBJECTPATH "/xswiftbus/service"
28 
29 namespace XSwiftBus
30 {
34  class CService : public CDBusObject
35  {
36  public:
38  CService(CSettingsProvider *settingsProvider);
39 
41  virtual ~CService() override;
42 
44  static const std::string &InterfaceName()
45  {
46  static const std::string s(XSWIFTBUS_SERVICE_INTERFACENAME);
47  return s;
48  }
49 
51  static const std::string &ObjectPath()
52  {
53  static const std::string s(XSWIFTBUS_SERVICE_OBJECTPATH);
54  return s;
55  }
56 
59 
61  void onSceneryLoaded();
62 
64  std::string getVersionNumber() const;
65 
67  std::string getCommitHash() const;
68 
70  void addTextMessage(const std::string &text, double red, double green, double blue);
71 
73  std::string getAircraftModelPath() const;
74 
76  std::string getAircraftModelFilename() const;
77 
79  std::string getAircraftModelString() const;
80 
82  std::string getAircraftName() const;
83 
85  std::string getAircraftLivery() const;
86 
88  std::string getAircraftIcaoCode() const { return m_icao.get(); }
89 
91  std::string getAircraftDescription() const { return m_descrip.get(); }
92 
94  int getXPlaneVersionMajor() const;
95 
97  int getXPlaneVersionMinor() const;
98 
100  std::string getXPlaneInstallationPath() const;
101 
103  std::string getXPlanePreferencesPath() const;
104 
106  bool isPaused() const { return m_paused.get(); }
107 
109  bool isUsingRealTime() const { return m_useSystemTime.get(); }
110 
117  std::tuple<double, double, double, double> getFrameStats();
118 
120  void resetFrameTotals();
121 
123  double getLatitudeDeg() const { return m_latitude.get(); }
124 
126  double getLongitudeDeg() const { return m_longitude.get(); }
127 
129  double getAltitudeMslM() const { return m_elevation.get(); }
130 
133  double getPressureAltitudeFt() const
134  {
135  return m_pressureAlt.isValid() ? m_pressureAlt.get() : std::numeric_limits<double>::quiet_NaN();
136  }
137 
139  double getHeightAglM() const { return m_agl.get(); }
140 
142  double getGroundSpeedMps() const { return m_groundSpeed.get(); }
143 
145  double getIndicatedAirspeedKias() const { return m_indicatedAirspeed.get(); }
146 
148  double getTrueAirspeedKias() const { return m_trueAirspeed.get(); }
149 
151  double getPitchDeg() const { return m_pitch.get(); }
152 
154  double getRollDeg() const { return m_roll.get(); }
155 
157  double getTrueHeadingDeg() const { return m_heading.get(); }
158 
161  double getLocalXVelocityMps() const { return isPaused() ? 0 : m_velocityX.get(); }
162  double getLocalYVelocityMps() const { return isPaused() ? 0 : m_velocityY.get(); }
163  double getLocalZVelocityMps() const { return isPaused() ? 0 : m_velocityZ.get(); }
165 
168  double getPitchRadPerSec() const { return isPaused() ? 0 : m_pitchVelocity.get(); }
169  double getRollRadPerSec() const { return isPaused() ? 0 : m_rollVelocity.get(); }
170  double getHeadingRadPerSec() const { return isPaused() ? 0 : m_headingVelocity.get(); }
172 
174  bool getAnyWheelOnGround() const { return m_onGroundAny.get(); }
175 
177  bool getAllWheelsOnGround() const { return m_onGroundAll.get(); }
178 
180  double getGroundElevation() const
181  {
182  return m_terrainProbe.getElevation(m_latitude.get(), m_longitude.get(), m_elevation.get())[0];
183  }
184 
187  int getComSelection() const { return m_comAudioSelection.get(); }
188  bool isCom1Selected() const { return this->getComSelection() == 6; }
189  bool isCom2Selected() const { return this->getComSelection() == 7; }
191 
193  int getCom1ActiveKhz() const { return m_com1Active.get(); }
194 
196  int getCom1StandbyKhz() const { return m_com1Standby.get(); }
197 
199  bool getCom1Power() const { return m_com1Power.get(); }
200 
202  bool getCom1Listening() const { return m_com1Listening.get(); }
203 
205  float getCom1Volume() const { return m_com1Volume.get(); }
206 
208  bool isCom1Receiving() const { return this->getCom1Power() && this->getCom1Listening(); }
209 
211  bool isCom1Transmitting() const { return this->getCom1Power() && this->isCom1Selected(); }
212 
214  int getCom2ActiveKhz() const { return m_com2Active.get(); }
215 
217  int getCom2StandbyKhz() const { return m_com2Standby.get(); }
218 
220  bool getCom2Power() const { return m_com2Power.get(); }
221 
223  bool getCom2Listening() const { return m_com2Listening.get(); }
224 
226  float getCom2Volume() const { return m_com2Volume.get(); }
227 
229  bool isCom2Receiving() const { return this->getCom2Power() && this->getCom2Listening(); }
230 
232  bool isCom2Transmitting() const { return this->getCom2Power() && this->isCom2Selected(); }
233 
235  int getTransponderCode() const { return m_xpdrCode.get(); }
236 
238  int getTransponderMode() const { return m_xpdrMode.get(); }
239 
241  bool getTransponderIdent() const { return m_xpdrIdent.get(); }
242 
244  bool getBeaconLightsOn() const { return m_beaconLightsOn.get(); }
245 
247  bool getLandingLightsOn() const { return m_landingLightsOn.get(); }
248 
250  bool getNavLightsOn() const { return m_navLightsOn.get(); }
251 
253  bool getStrobeLightsOn() const { return m_strobeLightsOn.get(); }
254 
256  bool getTaxiLightsOn() const { return m_taxiLightsOn.get(); }
257 
259  double getQNHInHg() const { return m_qnhInhg.get(); }
260 
262  void setCom1ActiveKhz(int freq) { m_com1Active.set(freq); }
263 
265  void setCom1StandbyKhz(int freq) { m_com1Standby.set(freq); }
266 
268  void setCom2ActiveKhz(int freq) { m_com2Active.set(freq); }
269 
271  void setCom2StandbyKhz(int freq) { m_com2Standby.set(freq); }
272 
274  void setTransponderCode(int code) { m_xpdrCode.set(code); }
275 
277  void setTransponderMode(int mode) { m_xpdrMode.set(mode); }
278 
280  double getFlapsDeployRatio() const { return m_flapsReployRatio.get(); }
281 
283  double getGearDeployRatio() const { return m_gearReployRatio.getAt(0); }
284 
286  int getNumberOfEngines() const { return m_numberOfEngines.get(); }
287 
289  std::vector<double> getEngineN1Percentage() const
290  {
291  std::vector<double> list;
292  const int number = getNumberOfEngines();
293  list.reserve(static_cast<std::vector<double>::size_type>(number));
294  for (int engineNumber = 0; engineNumber < number; ++engineNumber)
295  {
296  list.push_back(m_enginesN1Percentage.getAt(engineNumber));
297  }
298  return list;
299  }
300 
303  double getSpeedBrakeRatio() const { return m_speedBrakeRatio.get(); }
304 
306  void toggleMessageBoxVisibility() { m_messages.toggle(); }
307 
309  void setPopupMessageWindow(bool enabled) { m_popupMessageWindow = enabled; }
310 
312  void setDisappearMessageWindow(bool enabled) { m_disappearMessageWindow = enabled; }
313 
315  void setDisappearMessageWindowTimeMs(int durationMs);
316 
318  std::string getSettingsJson() const;
319 
321  void setSettingsJson(const std::string &jsonString);
322 
324  int process();
325 
326  protected:
327  DBusHandlerResult dbusMessageHandler(const CDBusMessage &message) override;
328 
329  private:
330  void emitAircraftModelChanged(const std::string &path, const std::string &filename, const std::string &livery,
331  const std::string &icao, const std::string &modelString, const std::string &name,
332  const std::string &description);
333 
334  void emitSceneryLoaded();
335 
336  CMessageBoxControl m_messages { 16, 16, 16 };
337  bool m_popupMessageWindow = true;
338  bool m_disappearMessageWindow = true;
339  int m_disapperMessageWindowTimeMs = 5000;
340  std::chrono::system_clock::time_point m_disappearMessageWindowTime;
341  CTerrainProbe m_terrainProbe;
342 
344  void updateMessageBoxFromSettings();
345 
346  struct FramePeriodSampler;
347  std::unique_ptr<FramePeriodSampler> m_framePeriodSampler;
348 
349  DataRef<xplane::data::sim::graphics::scenery::async_scenery_load_in_progress> m_sceneryIsLoading;
350  int m_sceneryWasLoading = 0;
351 
352  StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
353  StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
354  StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;
355  DataRef<xplane::data::sim::time::paused> m_paused;
356  DataRef<xplane::data::sim::time::use_system_time> m_useSystemTime;
357  DataRef<xplane::data::sim::flightmodel::position::latitude> m_latitude;
358  DataRef<xplane::data::sim::flightmodel::position::longitude> m_longitude;
359  DataRef<xplane::data::sim::flightmodel::position::elevation> m_elevation;
360  DataRef<xplane::data::sim::flightmodel2::position::pressure_altitude> m_pressureAlt;
361  DataRef<xplane::data::sim::flightmodel::position::y_agl> m_agl;
362  DataRef<xplane::data::sim::flightmodel::position::groundspeed> m_groundSpeed;
363  DataRef<xplane::data::sim::flightmodel::position::indicated_airspeed2> m_indicatedAirspeed;
364  DataRef<xplane::data::sim::flightmodel::position::true_airspeed> m_trueAirspeed;
365  DataRef<xplane::data::sim::flightmodel::position::theta> m_pitch;
366  DataRef<xplane::data::sim::flightmodel::position::phi> m_roll;
367  DataRef<xplane::data::sim::flightmodel::position::psi> m_heading;
368  DataRef<xplane::data::sim::flightmodel::position::local_vx> m_velocityX;
369  DataRef<xplane::data::sim::flightmodel::position::local_vy> m_velocityY;
370  DataRef<xplane::data::sim::flightmodel::position::local_vz> m_velocityZ;
371  DataRef<xplane::data::sim::flightmodel::position::Prad> m_rollVelocity;
372  DataRef<xplane::data::sim::flightmodel::position::Qrad> m_pitchVelocity;
373  DataRef<xplane::data::sim::flightmodel::position::Rrad> m_headingVelocity;
374  DataRef<xplane::data::sim::flightmodel::failures::onground_any> m_onGroundAny;
375  DataRef<xplane::data::sim::flightmodel::failures::onground_all> m_onGroundAll;
376  DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_frequency_hz_833> m_com1Active;
377  DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_standby_frequency_hz_833> m_com1Standby;
378  DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_com_selection>
379  m_comAudioSelection; // 6==COM1, 7==COM2
380  DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_power> m_com1Power;
381  DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_volume_com1> m_com1Volume; // 0..1
382  DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_selection_com1> m_com1Listening;
383  DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_frequency_hz_833> m_com2Active;
384  DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_standby_frequency_hz_833> m_com2Standby;
385  DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_power> m_com2Power;
386  DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_volume_com2> m_com2Volume; // 0..1
387  DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_selection_com2> m_com2Listening;
388  DataRef<xplane::data::sim::cockpit::radios::transponder_code> m_xpdrCode;
389  DataRef<xplane::data::sim::cockpit::radios::transponder_mode> m_xpdrMode;
390  DataRef<xplane::data::sim::cockpit::radios::transponder_id> m_xpdrIdent;
391  DataRef<xplane::data::sim::cockpit::electrical::beacon_lights_on> m_beaconLightsOn;
392  DataRef<xplane::data::sim::cockpit::electrical::landing_lights_on> m_landingLightsOn;
393  DataRef<xplane::data::sim::cockpit::electrical::nav_lights_on> m_navLightsOn;
394  DataRef<xplane::data::sim::cockpit::electrical::strobe_lights_on> m_strobeLightsOn;
395  DataRef<xplane::data::sim::cockpit::electrical::taxi_light_on> m_taxiLightsOn;
396  DataRef<xplane::data::sim::flightmodel2::controls::flap_handle_deploy_ratio> m_flapsReployRatio;
397  ArrayDataRef<xplane::data::sim::flightmodel2::gear::deploy_ratio> m_gearReployRatio;
398  DataRef<xplane::data::sim::aircraft::engine::acf_num_engines> m_numberOfEngines;
399  ArrayDataRef<xplane::data::sim::flightmodel::engine::ENGN_N1_> m_enginesN1Percentage;
400  DataRef<xplane::data::sim::flightmodel2::controls::speedbrake_ratio> m_speedBrakeRatio;
401  DataRef<xplane::data::sim::weather::barometer_sealevel_inhg> m_qnhInhg;
402  };
403 } // namespace XSwiftBus
404 
405 #endif // SWIFT_SIM_XSWIFTBUS_SERVICE_H
DataRefType getAt(int index) const
Get the value of a single element.
Definition: datarefs.h:168
DBus base object.
Definition: dbusobject.h:20
Class which builds upon CMessageBox with a scrollback buffer and commands for user control.
Definition: messages.h:111
void toggle()
Toggles the visibility of the message box.
Definition: messages.h:133
xswiftbus service object which is accessible through DBus
Definition: service.h:35
int getXPlaneVersionMinor() const
Get minor version number.
Definition: service.cpp:224
bool isCom1Transmitting() const
Is COM1 transmitting?
Definition: service.h:211
std::string getAircraftName() const
Get name of current aircraft model.
Definition: service.cpp:197
double getRollDeg() const
Get aircraft roll in degrees.
Definition: service.h:154
bool getCom2Power() const
Get the COM2 power on/off.
Definition: service.h:220
float getCom1Volume() const
Get the COM1 volume 0..1.
Definition: service.h:205
double getFlapsDeployRatio() const
Get flaps deploy ratio, where 0.0 is flaps fully retracted, and 1.0 is flaps fully extended.
Definition: service.h:280
float getCom2Volume() const
Get the COM2 volume 0..1.
Definition: service.h:226
int getCom1ActiveKhz() const
Get the current COM1 active frequency in kHz.
Definition: service.h:193
void toggleMessageBoxVisibility()
Toggles the visibility of the message box.
Definition: service.h:306
void setDisappearMessageWindow(bool enabled)
Enable/disable message window disappearing again after x ms.
Definition: service.h:312
virtual ~CService()
Destructor.
double getLongitudeDeg() const
Get aircraft longitude in degrees.
Definition: service.h:126
double getAltitudeMslM() const
Get aircraft altitude in meters.
Definition: service.h:129
double getGroundSpeedMps() const
Get aircraft groundspeed in meters per second.
Definition: service.h:142
std::string getAircraftIcaoCode() const
Get the ICAO code of the current aircraft model.
Definition: service.h:88
std::string getAircraftModelString() const
Get canonical swift model string of current aircraft model.
Definition: service.cpp:188
int getComSelection() const
COM Selection 6/7.
Definition: service.h:187
bool isPaused() const
True if sim is paused.
Definition: service.h:106
int getCom2StandbyKhz() const
Get the current COM2 standby frequency in kHz.
Definition: service.h:217
double getTrueAirspeedKias() const
Get aircraft TAS in meters per second.
Definition: service.h:148
double getRollRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition: service.h:169
int getXPlaneVersionMajor() const
Get major version number.
Definition: service.cpp:216
bool isCom2Receiving() const
Is COM2 receiving?
Definition: service.h:229
std::tuple< double, double, double, double > getFrameStats()
Frames-per-second, averaged over the last 500 frames, or since this function was last called,...
Definition: service.cpp:121
void resetFrameTotals()
Reset the monitoring of total miles and minutes lost due to low frame rate.
Definition: service.cpp:129
double getLocalZVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition: service.h:163
CService(CSettingsProvider *settingsProvider)
Constructor.
Definition: service.cpp:91
DBusHandlerResult dbusMessageHandler(const CDBusMessage &message)
DBus message handler.
Definition: service.cpp:265
bool getTaxiLightsOn() const
Get whether taxi lights are on.
Definition: service.h:256
double getPitchRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition: service.h:168
double getSpeedBrakeRatio() const
Get the ratio how much the speedbrakes surfaces are extended (0.0 is fully retracted,...
Definition: service.h:303
double getHeadingRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition: service.h:170
double getPitchDeg() const
Get aircraft pitch in degrees above horizon.
Definition: service.h:151
std::string getXPlaneInstallationPath() const
Get root of X-Plane install path.
Definition: service.cpp:232
std::string getCommitHash() const
Returns the SHA1 of the last commit that could influence xswiftbus.
Definition: service.cpp:119
void onAircraftModelChanged()
Called by XPluginReceiveMessage when the model changes.
Definition: service.cpp:100
int getTransponderCode() const
Get the current transponder code in decimal.
Definition: service.h:235
std::string getXPlanePreferencesPath() const
Get full path to X-Plane preferences file.
Definition: service.cpp:239
double getGearDeployRatio() const
Get gear deploy ratio, where 0 is up and 1 is down.
Definition: service.h:283
double getLocalYVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition: service.h:162
bool getTransponderIdent() const
Get whether we are currently squawking ident.
Definition: service.h:241
bool isCom2Selected() const
COM Selection 6/7.
Definition: service.h:189
bool getCom2Listening() const
Get the COM2 listening yes/no.
Definition: service.h:223
int getCom1StandbyKhz() const
Get the current COM1 standby frequency in kHz.
Definition: service.h:196
bool getCom1Listening() const
Get the COM1 listening yes/no.
Definition: service.h:202
bool getAnyWheelOnGround() const
Get whether any wheel is on the ground.
Definition: service.h:174
void setTransponderMode(int mode)
Set the current transponder mode (depends on the aircraft, 0 and 1 usually mean standby,...
Definition: service.h:277
void setCom1StandbyKhz(int freq)
Set the current COM1 standby frequency in kHz.
Definition: service.h:265
bool getCom1Power() const
Get the COM1 power on/off.
Definition: service.h:199
std::string getVersionNumber() const
Returns the xswiftbus version number.
Definition: service.cpp:117
bool getLandingLightsOn() const
Get whether landing lights are on.
Definition: service.h:247
bool isCom1Receiving() const
Is COM1 receiving?
Definition: service.h:208
void onSceneryLoaded()
Called by XPluginReceiveMessage when some scenery is loaded.
Definition: service.cpp:115
double getHeightAglM() const
Get aircraft height in meters.
Definition: service.h:139
std::string getAircraftDescription() const
Get the description of the current aircraft model.
Definition: service.h:91
void setTransponderCode(int code)
Set the current transponder code in decimal.
Definition: service.h:274
void setDisappearMessageWindowTimeMs(int durationMs)
Enable/disable message window disappearing after x ms.
Definition: service.cpp:246
static const std::string & ObjectPath()
DBus object path.
Definition: service.h:51
void setPopupMessageWindow(bool enabled)
Enable/disable message window popping up for new messages.
Definition: service.h:309
double getIndicatedAirspeedKias() const
Get aircraft IAS in knots.
Definition: service.h:145
void setCom2ActiveKhz(int freq)
Set the current COM2 active frequency in kHz.
Definition: service.h:268
double getLocalXVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition: service.h:161
bool getBeaconLightsOn() const
Get whether beacon lights are on.
Definition: service.h:244
int process()
Perform generic processing.
Definition: service.cpp:759
double getPressureAltitudeFt() const
Get aircraft pressure altitude in feet in standard atmosphere in X-Plane 12. NaN in earlier versions ...
Definition: service.h:133
bool getStrobeLightsOn() const
Get whether strobe lights are on.
Definition: service.h:253
std::string getAircraftModelPath() const
Get full path to current aircraft model.
Definition: service.cpp:172
bool isCom1Selected() const
COM Selection 6/7.
Definition: service.h:188
void addTextMessage(const std::string &text, double red, double green, double blue)
Add a text message to the on-screen display, with RGB components in the range [0,1].
Definition: service.cpp:138
static const std::string & InterfaceName()
DBus interface name.
Definition: service.h:44
double getLatitudeDeg() const
Get aircraft latitude in degrees.
Definition: service.h:123
int getCom2ActiveKhz() const
Get the current COM2 active frequency in kHz.
Definition: service.h:214
bool isCom2Transmitting() const
Is COM2 transmitting?
Definition: service.h:232
double getTrueHeadingDeg() const
Get aircraft true heading in degrees.
Definition: service.h:157
std::string getAircraftLivery() const
Get current aircraft livery.
Definition: service.cpp:206
double getQNHInHg() const
Get barometric pressure at sea level in inches of mercury.
Definition: service.h:259
void setCom1ActiveKhz(int freq)
Set the current COM1 active frequency in kHz.
Definition: service.h:262
void setCom2StandbyKhz(int freq)
Set the current COM2 standby frequency in kHz.
Definition: service.h:271
int getTransponderMode() const
Get the current transponder mode (depends on the aircraft, 0 and 1 usually mean standby,...
Definition: service.h:238
int getNumberOfEngines() const
Get the number of engines of current aircraft.
Definition: service.h:286
std::string getSettingsJson() const
Get settings in JSON format.
Definition: service.cpp:248
bool isUsingRealTime() const
True if sim time is tracking operating system time.
Definition: service.h:109
double getGroundElevation() const
Get elevation of ground under the plane in meters.
Definition: service.h:180
std::vector< double > getEngineN1Percentage() const
Get the N1 speed as percent of max (per engine)
Definition: service.h:289
std::string getAircraftModelFilename() const
Get base filename of current aircraft model.
Definition: service.cpp:180
void setSettingsJson(const std::string &jsonString)
Set settings.
Definition: service.cpp:250
bool getAllWheelsOnGround() const
Get whether all wheels are on the ground.
Definition: service.h:177
bool getNavLightsOn() const
Get whether nav lights are on.
Definition: service.h:250
Something owning the settings.
Definition: settings.h:31
std::array< double, 3 > getElevation(double degreesLatitude, double degreesLongitude, double metersAltitude) const
Get the elevation in meters at the given point in OpenGL space.
void set(DataRefType d)
Set the value of the dataref (if it is writable)
Definition: datarefs.h:104
DataRefType get() const
Get the value of the dataref.
Definition: datarefs.h:117
std::string get() const
Get the value of the whole string.
Definition: datarefs.h:202
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14