swift
Loading...
Searching...
No Matches
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 "custom_datarefs.h"
19#include <XPLM/XPLMNavigation.h>
20#include <string>
21#include <chrono>
22#include <limits>
23// clang-format on
24
26#define XSWIFTBUS_SERVICE_INTERFACENAME "org.swift_project.xswiftbus.service"
27#define XSWIFTBUS_SERVICE_OBJECTPATH "/xswiftbus/service"
29
30namespace XSwiftBus
31{
35 class CService : public CDBusObject
36 {
37 public:
39 CService(CSettingsProvider *settingsProvider);
40
42 virtual ~CService() override;
43
45 static const std::string &InterfaceName()
46 {
47 static const std::string s(XSWIFTBUS_SERVICE_INTERFACENAME);
48 return s;
49 }
50
52 static const std::string &ObjectPath()
53 {
54 static const std::string s(XSWIFTBUS_SERVICE_OBJECTPATH);
55 return s;
56 }
57
60
62 void onSceneryLoaded();
63
65 static std::string getVersionNumber();
66
68 static std::string getCommitHash();
69
71 void addTextMessage(const std::string &text, double red, double green, double blue);
72
74 static std::string getAircraftModelPath();
75
77 static std::string getAircraftModelFilename();
78
80 static std::string getAircraftModelString();
81
83 static std::string getAircraftName();
84
86 std::string getAircraftLivery() const;
87
89 std::string getAircraftIcaoCode() const { return m_icao.get(); }
90
92 std::string getAircraftDescription() const { return m_descrip.get(); }
93
95 static int getXPlaneVersionMajor();
96
98 static int getXPlaneVersionMinor();
99
101 static std::string getXPlaneInstallationPath();
102
104 static std::string getXPlanePreferencesPath();
105
107 bool isPaused() const { return m_paused.get(); }
108
110 bool isUsingRealTime() const { return m_useSystemTime.get(); }
111
118 std::tuple<double, double, double, double> getFrameStats();
119
121 void resetFrameTotals();
122
124 void setFlightNetworkConnected(bool connected);
125
127 void setOwnCallsign(const std::string &callsign);
128
130 double getLatitudeDeg() const { return m_latitude.get(); }
131
133 double getLongitudeDeg() const { return m_longitude.get(); }
134
136 double getAltitudeMslM() const { return m_elevation.get(); }
137
141 {
142 return m_pressureAlt.isValid() ? m_pressureAlt.get() : std::numeric_limits<double>::quiet_NaN();
143 }
144
146 double getHeightAglM() const { return m_agl.get(); }
147
149 double getGroundSpeedMps() const { return m_groundSpeed.get(); }
150
152 double getIndicatedAirspeedKias() const { return m_indicatedAirspeed.get(); }
153
155 double getTrueAirspeedKias() const { return m_trueAirspeed.get(); }
156
158 double getPitchDeg() const { return m_pitch.get(); }
159
161 double getRollDeg() const { return m_roll.get(); }
162
164 double getTrueHeadingDeg() const { return m_heading.get(); }
165
168 double getLocalXVelocityMps() const { return isPaused() ? 0 : m_velocityX.get(); }
169 double getLocalYVelocityMps() const { return isPaused() ? 0 : m_velocityY.get(); }
170 double getLocalZVelocityMps() const { return isPaused() ? 0 : m_velocityZ.get(); }
172
175 double getPitchRadPerSec() const { return isPaused() ? 0 : m_pitchVelocity.get(); }
176 double getRollRadPerSec() const { return isPaused() ? 0 : m_rollVelocity.get(); }
177 double getHeadingRadPerSec() const { return isPaused() ? 0 : m_headingVelocity.get(); }
179
181 bool getAnyWheelOnGround() const { return m_onGroundAny.get(); }
182
184 bool getAllWheelsOnGround() const { return m_onGroundAll.get(); }
185
187 double getGroundElevation() const
188 {
189 return m_terrainProbe.getElevation(m_latitude.get(), m_longitude.get(), m_elevation.get())[0];
190 }
191
194 int getComSelection() const { return m_comAudioSelection.get(); }
195 bool isCom1Selected() const { return this->getComSelection() == 6; }
196 bool isCom2Selected() const { return this->getComSelection() == 7; }
198
200 int getCom1ActiveKhz() const { return m_com1Active.get(); }
201
203 int getCom1StandbyKhz() const { return m_com1Standby.get(); }
204
206 bool getCom1Power() const { return m_com1Power.get(); }
207
209 bool getCom1Listening() const { return m_com1Listening.get(); }
210
212 float getCom1Volume() const { return m_com1Volume.get(); }
213
215 bool isCom1Receiving() const { return this->getCom1Power() && this->getCom1Listening(); }
216
218 bool isCom1Transmitting() const { return this->getCom1Power() && this->isCom1Selected(); }
219
221 int getCom2ActiveKhz() const { return m_com2Active.get(); }
222
224 int getCom2StandbyKhz() const { return m_com2Standby.get(); }
225
227 bool getCom2Power() const { return m_com2Power.get(); }
228
230 bool getCom2Listening() const { return m_com2Listening.get(); }
231
233 float getCom2Volume() const { return m_com2Volume.get(); }
234
236 bool isCom2Receiving() const { return this->getCom2Power() && this->getCom2Listening(); }
237
239 bool isCom2Transmitting() const { return this->getCom2Power() && this->isCom2Selected(); }
240
242 int getTransponderCode() const { return m_xpdrCode.get(); }
243
245 int getTransponderMode() const { return m_xpdrMode.get(); }
246
248 bool getTransponderIdent() const { return m_xpdrIdent.get(); }
249
251 bool getBeaconLightsOn() const { return m_beaconLightsOn.get(); }
252
254 bool getLandingLightsOn() const { return m_landingLightsOn.get(); }
255
257 bool getNavLightsOn() const { return m_navLightsOn.get(); }
258
260 bool getStrobeLightsOn() const { return m_strobeLightsOn.get(); }
261
263 bool getTaxiLightsOn() const { return m_taxiLightsOn.get(); }
264
266 double getQNHInHg() const { return m_qnhInhg.get(); }
267
269 void setCom1ActiveKhz(int freq) { m_com1Active.set(freq); }
270
272 void setCom1StandbyKhz(int freq) { m_com1Standby.set(freq); }
273
275 void setCom2ActiveKhz(int freq) { m_com2Active.set(freq); }
276
278 void setCom2StandbyKhz(int freq) { m_com2Standby.set(freq); }
279
281 void setTransponderCode(int code) { m_xpdrCode.set(code); }
282
284 void setTransponderMode(int mode) { m_xpdrMode.set(mode); }
285
287 double getFlapsDeployRatio() const { return m_flapsReployRatio.get(); }
288
290 double getGearDeployRatio() const { return m_gearReployRatio.getAt(0); }
291
293 int getNumberOfEngines() const { return m_numberOfEngines.get(); }
294
296 std::vector<double> getEngineN1Percentage() const
297 {
298 std::vector<double> list;
299 const int number = getNumberOfEngines();
300 list.reserve(static_cast<std::vector<double>::size_type>(number));
301 for (int engineNumber = 0; engineNumber < number; ++engineNumber)
302 {
303 list.push_back(m_enginesN1Percentage.getAt(engineNumber));
304 }
305 return list;
306 }
307
310 double getSpeedBrakeRatio() const { return m_speedBrakeRatio.get(); }
311
313 void toggleMessageBoxVisibility() { m_messages.toggle(); }
314
316 void setPopupMessageWindow(bool enabled) { m_popupMessageWindow = enabled; }
317
319 void setDisappearMessageWindow(bool enabled) { m_disappearMessageWindow = enabled; }
320
322 void setDisappearMessageWindowTimeMs(int durationMs);
323
325 std::string getSettingsJson() const;
326
328 void setSettingsJson(const std::string &jsonString);
329
331 int process();
332
333 protected:
334 DBusHandlerResult dbusMessageHandler(const CDBusMessage &message) override;
335
336 private:
337 void emitAircraftModelChanged(const std::string &path, const std::string &filename, const std::string &livery,
338 const std::string &icao, const std::string &modelString, const std::string &name,
339 const std::string &description);
340
341 void emitSceneryLoaded();
342
343 CMessageBoxControl m_messages { 16, 16, 16 };
344 bool m_popupMessageWindow = true;
345 bool m_disappearMessageWindow = true;
346 int m_disapperMessageWindowTimeMs = 5000;
347 std::chrono::system_clock::time_point m_disappearMessageWindowTime;
348 CTerrainProbe m_terrainProbe;
349
351 void updateMessageBoxFromSettings();
352
353 struct FramePeriodSampler;
354 std::unique_ptr<FramePeriodSampler> m_framePeriodSampler;
355
356 DataRef<xplane::data::sim::graphics::scenery::async_scenery_load_in_progress> m_sceneryIsLoading;
357 int m_sceneryWasLoading = 0;
358
359 CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected;
360 CustomDataRef<TSwiftCallsign> m_swiftCallsign;
361 StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
362 StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
363 StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;
364 DataRef<xplane::data::sim::time::paused> m_paused;
365 DataRef<xplane::data::sim::time::use_system_time> m_useSystemTime;
366 DataRef<xplane::data::sim::flightmodel::position::latitude> m_latitude;
367 DataRef<xplane::data::sim::flightmodel::position::longitude> m_longitude;
368 DataRef<xplane::data::sim::flightmodel::position::elevation> m_elevation;
369 DataRef<xplane::data::sim::flightmodel2::position::pressure_altitude> m_pressureAlt;
370 DataRef<xplane::data::sim::flightmodel::position::y_agl> m_agl;
371 DataRef<xplane::data::sim::flightmodel::position::groundspeed> m_groundSpeed;
372 DataRef<xplane::data::sim::flightmodel::position::indicated_airspeed2> m_indicatedAirspeed;
373 DataRef<xplane::data::sim::flightmodel::position::true_airspeed> m_trueAirspeed;
374 DataRef<xplane::data::sim::flightmodel::position::theta> m_pitch;
375 DataRef<xplane::data::sim::flightmodel::position::phi> m_roll;
376 DataRef<xplane::data::sim::flightmodel::position::psi> m_heading;
377 DataRef<xplane::data::sim::flightmodel::position::local_vx> m_velocityX;
378 DataRef<xplane::data::sim::flightmodel::position::local_vy> m_velocityY;
379 DataRef<xplane::data::sim::flightmodel::position::local_vz> m_velocityZ;
380 DataRef<xplane::data::sim::flightmodel::position::Prad> m_rollVelocity;
381 DataRef<xplane::data::sim::flightmodel::position::Qrad> m_pitchVelocity;
382 DataRef<xplane::data::sim::flightmodel::position::Rrad> m_headingVelocity;
383 DataRef<xplane::data::sim::flightmodel::failures::onground_any> m_onGroundAny;
384 DataRef<xplane::data::sim::flightmodel::failures::onground_all> m_onGroundAll;
385 DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_frequency_hz_833> m_com1Active;
386 DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_standby_frequency_hz_833> m_com1Standby;
387 DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_com_selection>
388 m_comAudioSelection; // 6==COM1, 7==COM2
389 DataRef<xplane::data::sim::cockpit2::radios::actuators::com1_power> m_com1Power;
390 DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_volume_com1> m_com1Volume; // 0..1
391 DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_selection_com1> m_com1Listening;
392 DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_frequency_hz_833> m_com2Active;
393 DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_standby_frequency_hz_833> m_com2Standby;
394 DataRef<xplane::data::sim::cockpit2::radios::actuators::com2_power> m_com2Power;
395 DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_volume_com2> m_com2Volume; // 0..1
396 DataRef<xplane::data::sim::cockpit2::radios::actuators::audio_selection_com2> m_com2Listening;
397 DataRef<xplane::data::sim::cockpit::radios::transponder_code> m_xpdrCode;
398 DataRef<xplane::data::sim::cockpit::radios::transponder_mode> m_xpdrMode;
399 DataRef<xplane::data::sim::cockpit::radios::transponder_id> m_xpdrIdent;
400 DataRef<xplane::data::sim::cockpit::electrical::beacon_lights_on> m_beaconLightsOn;
401 DataRef<xplane::data::sim::cockpit::electrical::landing_lights_on> m_landingLightsOn;
402 DataRef<xplane::data::sim::cockpit::electrical::nav_lights_on> m_navLightsOn;
403 DataRef<xplane::data::sim::cockpit::electrical::strobe_lights_on> m_strobeLightsOn;
404 DataRef<xplane::data::sim::cockpit::electrical::taxi_light_on> m_taxiLightsOn;
405 DataRef<xplane::data::sim::flightmodel2::controls::flap_handle_deploy_ratio> m_flapsReployRatio;
406 ArrayDataRef<xplane::data::sim::flightmodel2::gear::deploy_ratio> m_gearReployRatio;
407 DataRef<xplane::data::sim::aircraft::engine::acf_num_engines> m_numberOfEngines;
408 ArrayDataRef<xplane::data::sim::flightmodel::engine::ENGN_N1_> m_enginesN1Percentage;
409 DataRef<xplane::data::sim::flightmodel2::controls::speedbrake_ratio> m_speedBrakeRatio;
410 DataRef<xplane::data::sim::weather::barometer_sealevel_inhg> m_qnhInhg;
411 };
412} // namespace XSwiftBus
413
414#endif // SWIFT_SIM_XSWIFTBUS_SERVICE_H
CDBusObject(CSettingsProvider *settingsProvider)
Constructor.
Class which builds upon CMessageBox with a scrollback buffer and commands for user control.
Definition messages.h:111
static const std::string & InterfaceName()
DBus interface name.
Definition service.h:45
bool isCom1Transmitting() const
Is COM1 transmitting?
Definition service.h:218
double getRollDeg() const
Get aircraft roll in degrees.
Definition service.h:161
bool getCom2Power() const
Get the COM2 power on/off.
Definition service.h:227
float getCom1Volume() const
Get the COM1 volume 0..1.
Definition service.h:212
double getFlapsDeployRatio() const
Get flaps deploy ratio, where 0.0 is flaps fully retracted, and 1.0 is flaps fully extended.
Definition service.h:287
static std::string getCommitHash()
Returns the SHA1 of the last commit that could influence xswiftbus.
Definition service.cpp:131
float getCom2Volume() const
Get the COM2 volume 0..1.
Definition service.h:233
int getCom1ActiveKhz() const
Get the current COM1 active frequency in kHz.
Definition service.h:200
void toggleMessageBoxVisibility()
Toggles the visibility of the message box.
Definition service.h:313
static const std::string & ObjectPath()
DBus object path.
Definition service.h:52
void setDisappearMessageWindow(bool enabled)
Enable/disable message window disappearing again after x ms.
Definition service.h:319
virtual ~CService()
Destructor.
double getLongitudeDeg() const
Get aircraft longitude in degrees.
Definition service.h:133
double getAltitudeMslM() const
Get aircraft altitude in meters.
Definition service.h:136
double getGroundSpeedMps() const
Get aircraft groundspeed in meters per second.
Definition service.h:149
std::string getAircraftIcaoCode() const
Get the ICAO code of the current aircraft model.
Definition service.h:89
static std::string getAircraftModelPath()
Get full path to current aircraft model.
Definition service.cpp:200
static int getXPlaneVersionMinor()
Get minor version number.
Definition service.cpp:258
int getComSelection() const
COM Selection 6/7.
Definition service.h:194
bool isPaused() const
True if sim is paused.
Definition service.h:107
int getCom2StandbyKhz() const
Get the current COM2 standby frequency in kHz.
Definition service.h:224
void setFlightNetworkConnected(bool connected)
Set the current connection state.
Definition service.cpp:153
double getTrueAirspeedKias() const
Get aircraft TAS in meters per second.
Definition service.h:155
double getRollRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition service.h:176
bool isCom2Receiving() const
Is COM2 receiving?
Definition service.h:236
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:133
void resetFrameTotals()
Reset the monitoring of total miles and minutes lost due to low frame rate.
Definition service.cpp:144
double getLocalZVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition service.h:170
CService(CSettingsProvider *settingsProvider)
Constructor.
Definition service.cpp:100
DBusHandlerResult dbusMessageHandler(const CDBusMessage &message)
DBus message handler.
Definition service.cpp:568
bool getTaxiLightsOn() const
Get whether taxi lights are on.
Definition service.h:263
double getPitchRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition service.h:175
double getSpeedBrakeRatio() const
Get the ratio how much the speedbrakes surfaces are extended (0.0 is fully retracted,...
Definition service.h:310
double getHeadingRadPerSec() const
Get aircraft angular velocity in radians per second.
Definition service.h:177
double getPitchDeg() const
Get aircraft pitch in degrees above horizon.
Definition service.h:158
void onAircraftModelChanged()
Called by XPluginReceiveMessage when the model changes.
Definition service.cpp:112
int getTransponderCode() const
Get the current transponder code in decimal.
Definition service.h:242
double getGearDeployRatio() const
Get gear deploy ratio, where 0 is up and 1 is down.
Definition service.h:290
double getLocalYVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition service.h:169
bool getTransponderIdent() const
Get whether we are currently squawking ident.
Definition service.h:248
bool isCom2Selected() const
COM Selection 6/7.
Definition service.h:196
bool getCom2Listening() const
Get the COM2 listening yes/no.
Definition service.h:230
int getCom1StandbyKhz() const
Get the current COM1 standby frequency in kHz.
Definition service.h:203
static std::string getVersionNumber()
Returns the xswiftbus version number.
Definition service.cpp:129
static int getXPlaneVersionMajor()
Get major version number.
Definition service.cpp:247
static std::string getXPlaneInstallationPath()
Get root of X-Plane install path.
Definition service.cpp:269
void setOwnCallsign(const std::string &callsign)
Set the current own callsign.
Definition service.cpp:155
bool getCom1Listening() const
Get the COM1 listening yes/no.
Definition service.h:209
bool getAnyWheelOnGround() const
Get whether any wheel is on the ground.
Definition service.h:181
void setTransponderMode(int mode)
Set the current transponder mode (depends on the aircraft, 0 and 1 usually mean standby,...
Definition service.h:284
void setCom1StandbyKhz(int freq)
Set the current COM1 standby frequency in kHz.
Definition service.h:272
bool getCom1Power() const
Get the COM1 power on/off.
Definition service.h:206
bool getLandingLightsOn() const
Get whether landing lights are on.
Definition service.h:254
std::vector< double > getEngineN1Percentage() const
Get the N1 speed as percent of max (per engine).
Definition service.h:296
bool isCom1Receiving() const
Is COM1 receiving?
Definition service.h:215
void onSceneryLoaded()
Called by XPluginReceiveMessage when some scenery is loaded.
Definition service.cpp:127
double getHeightAglM() const
Get aircraft height in meters.
Definition service.h:146
std::string getAircraftDescription() const
Get the description of the current aircraft model.
Definition service.h:92
void setTransponderCode(int code)
Set the current transponder code in decimal.
Definition service.h:281
void setDisappearMessageWindowTimeMs(int durationMs)
Enable/disable message window disappearing after x ms.
Definition service.cpp:283
void setPopupMessageWindow(bool enabled)
Enable/disable message window popping up for new messages.
Definition service.h:316
double getIndicatedAirspeedKias() const
Get aircraft IAS in knots.
Definition service.h:152
void setCom2ActiveKhz(int freq)
Set the current COM2 active frequency in kHz.
Definition service.h:275
double getLocalXVelocityMps() const
Get aircraft local velocity in world coordinates meters per second.
Definition service.h:168
bool getBeaconLightsOn() const
Get whether beacon lights are on.
Definition service.h:251
int process()
Perform generic processing.
Definition service.cpp:1081
double getPressureAltitudeFt() const
Get aircraft pressure altitude in feet in standard atmosphere in X-Plane 12. NaN in earlier versions ...
Definition service.h:140
bool getStrobeLightsOn() const
Get whether strobe lights are on.
Definition service.h:260
bool isCom1Selected() const
COM Selection 6/7.
Definition service.h:195
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:157
double getLatitudeDeg() const
Get aircraft latitude in degrees.
Definition service.h:130
int getCom2ActiveKhz() const
Get the current COM2 active frequency in kHz.
Definition service.h:221
bool isCom2Transmitting() const
Is COM2 transmitting?
Definition service.h:239
static std::string getAircraftModelFilename()
Get base filename of current aircraft model.
Definition service.cpp:208
double getTrueHeadingDeg() const
Get aircraft true heading in degrees.
Definition service.h:164
static std::string getXPlanePreferencesPath()
Get full path to X-Plane preferences file.
Definition service.cpp:276
std::string getAircraftLivery() const
Get current aircraft livery.
Definition service.cpp:234
double getQNHInHg() const
Get barometric pressure at sea level in inches of mercury.
Definition service.h:266
static std::string getAircraftModelString()
Get canonical swift model string of current aircraft model.
Definition service.cpp:216
void setCom1ActiveKhz(int freq)
Set the current COM1 active frequency in kHz.
Definition service.h:269
void setCom2StandbyKhz(int freq)
Set the current COM2 standby frequency in kHz.
Definition service.h:278
int getTransponderMode() const
Get the current transponder mode (depends on the aircraft, 0 and 1 usually mean standby,...
Definition service.h:245
int getNumberOfEngines() const
Get the number of engines of current aircraft.
Definition service.h:293
std::string getSettingsJson() const
Get settings in JSON format.
Definition service.cpp:285
bool isUsingRealTime() const
True if sim time is tracking operating system time.
Definition service.h:110
double getGroundElevation() const
Get elevation of ground under the plane in meters.
Definition service.h:187
static std::string getAircraftName()
Get name of current aircraft model.
Definition service.cpp:225
void setSettingsJson(const std::string &jsonString)
Set settings.
Definition service.cpp:287
bool getAllWheelsOnGround() const
Get whether all wheels are on the ground.
Definition service.h:184
bool getNavLightsOn() const
Get whether nav lights are on.
Definition service.h:257
Something owning the settings.
Definition settings.h:31
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14