Debian Codegraph @

src/Network/Swift/service.cpp

raw symbols SCIP
1 /*
2 * Service module for swift<->FG connection
3 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
4 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <[email protected]>
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "service.h"
9 #include <Main/fg_props.hxx>
10 #include <iostream>
11 #include <simgear/constants.h>
12 #include <simgear/debug/logstream.hxx>
13 #include <simgear/structure/commands.hxx>
14
15 #define FGSWIFTBUS_API_VERSION 3;
16
17 namespace flightgear::swift {
18
19 static const std::string k_fgswiftbus_service_interfacename = "org.swift_project.fgswiftbus.service";
20 static const std::string k_fgswiftbus_service_objectpath = "/fgswiftbus/service";
21
22 CService::CService()
23 {
24 // Initialize node pointers
25 m_textMessageNode = fgGetNode("/sim/messages/copilot", true);
26 m_aircraftModelPathNode = fgGetNode("/sim/aircraft-dir", true);
27 m_aircraftDescriptionNode = fgGetNode("/sim/description", true);
28 m_isPausedNode = fgGetNode("/sim/freeze/master", true);
29 m_latitudeNode = fgGetNode("/position/latitude-deg", true);
30 m_longitudeNode = fgGetNode("/position/longitude-deg", true);
31 m_altitudeMSLNode = fgGetNode("/position/altitude-ft", true);
32 m_heightAGLNode = fgGetNode("/position/altitude-agl-ft", true);
33 m_groundSpeedNode = fgGetNode("/velocities/groundspeed-kt", true);
34 m_pitchNode = fgGetNode("/orientation/pitch-deg", true);
35 m_rollNode = fgGetNode("/orientation/roll-deg", true);
36 m_trueHeadingNode = fgGetNode("/orientation/heading-deg", true);
37 m_wheelsOnGroundNode = fgGetNode("/gear/gear/wow", true);
38 m_com1ActiveNode = fgGetNode("/instrumentation/comm/frequencies/selected-mhz", true);
39 m_com1StandbyNode = fgGetNode("/instrumentation/comm/frequencies/standby-mhz", true);
40 m_com2ActiveNode = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
41 m_com2StandbyNode = fgGetNode("/instrumentation/comm[1]/frequencies/standby-mhz", true);
42 m_transponderCodeNode = fgGetNode("/instrumentation/transponder/id-code", true);
43 m_transponderModeNode = fgGetNode("/instrumentation/transponder/inputs/knob-mode", true);
44 m_transponderIdentNode = fgGetNode("/instrumentation/transponder/ident", true);
45 m_beaconLightsNode = fgGetNode("/controls/lighting/beacon", true);
46 m_landingLightsNode = fgGetNode("/controls/lighting/landing-lights", true);
47 m_navLightsNode = fgGetNode("/controls/lighting/nav-lights", true);
48 m_strobeLightsNode = fgGetNode("/controls/lighting/strobe", true);
49 m_taxiLightsNode = fgGetNode("/controls/lighting/taxi-light", true);
50 m_altimeterServiceableNode = fgGetNode("/instrumentation/altimeter/serviceable", true);
51 m_pressAltitudeFtNode = fgGetNode("/instrumentation/altimeter/pressure-alt-ft", true);
52 m_flapsDeployRatioNode = fgGetNode("/surface-positions/flap-pos-norm", true);
53 m_gearDeployRatioNode = fgGetNode("/gear/gear/position-norm", true);
54 m_speedBrakeDeployRatioNode = fgGetNode("/surface-positions/speedbrake-pos-norm", true);
55 m_aircraftNameNode = fgGetNode("/sim/aircraft", true);
56 m_groundElevationNode = fgGetNode("/position/ground-elev-m", true);
57 m_velocityXNode = fgGetNode("/velocities/speed-east-fps", true);
58 m_velocityYNode = fgGetNode("/velocities/speed-down-fps", true);
59 m_velocityZNode = fgGetNode("/velocities/speed-north-fps", true);
60 m_rollRateNode = fgGetNode("/orientation/roll-rate-degps", true);
61 m_pichRateNode = fgGetNode("/orientation/pitch-rate-degps", true);
62 m_yawRateNode = fgGetNode("/orientation/yaw-rate-degps", true);
63 m_com1VolumeNode = fgGetNode("/instrumentation/comm/volume", true);
64 m_com2VolumeNode = fgGetNode("/instrumentation/comm[1]/volume", true);
65
66 SG_LOG(SG_NETWORK, SG_INFO, "FGSwiftBus Service initialized");
67 }
68
69 const std::string& CService::InterfaceName()
70 {
71 return k_fgswiftbus_service_interfacename;
72 }
73
74 const std::string& CService::ObjectPath()
75 {
76 return k_fgswiftbus_service_objectpath;
77 }
78
79 // Static method
80 int CService::getVersionNumber()
81 {
82 return FGSWIFTBUS_API_VERSION;
83 }
84
85 void CService::addTextMessage(const std::string& text)
86 {
87 if (text.empty()) { return; }
88 m_textMessageNode->setStringValue(text);
89 }
90
91 std::string CService::getAircraftModelPath() const
92 {
93 return m_aircraftModelPathNode->getStringValue();
94 }
95
96 std::string CService::getAircraftLivery() const
97 {
98 return "";
99 }
100
101 std::string CService::getAircraftIcaoCode() const
102 {
103 return "";
104 }
105
106 std::string CService::getAircraftDescription() const
107 {
108 return m_aircraftDescriptionNode->getStringValue();
109 }
110
111 bool CService::isPaused() const
112 {
113 return m_isPausedNode->getBoolValue();
114 }
115
116 double CService::getLatitude() const
117 {
118 return m_latitudeNode->getDoubleValue();
119 }
120
121 double CService::getLongitude() const
122 {
123 return m_longitudeNode->getDoubleValue();
124 }
125
126 double CService::getAltitudeMSL() const
127 {
128 return m_altitudeMSLNode->getDoubleValue();
129 }
130
131 double CService::getHeightAGL() const
132 {
133 return m_heightAGLNode->getDoubleValue();
134 }
135
136 double CService::getGroundSpeed() const
137 {
138 return m_groundSpeedNode->getDoubleValue();
139 }
140
141 double CService::getPitch() const
142 {
143 return m_pitchNode->getDoubleValue();
144 }
145
146 double CService::getRoll() const
147 {
148 return m_rollNode->getDoubleValue();
149 }
150
151 double CService::getTrueHeading() const
152 {
153 return m_trueHeadingNode->getDoubleValue();
154 }
155
156 bool CService::getAllWheelsOnGround() const
157 {
158 return m_wheelsOnGroundNode->getBoolValue();
159 }
160
161 int CService::getCom1Active() const
162 {
163 return (int)(m_com1ActiveNode->getDoubleValue() * 1000);
164 }
165
166 int CService::getCom1Standby() const
167 {
168 return (int)(m_com1StandbyNode->getDoubleValue() * 1000);
169 }
170
171 int CService::getCom2Active() const
172 {
173 return (int)(m_com2ActiveNode->getDoubleValue() * 1000);
174 }
175
176 int CService::getCom2Standby() const
177 {
178 return (int)(m_com2StandbyNode->getDoubleValue() * 1000);
179 }
180
181 int CService::getTransponderCode() const
182 {
183 return m_transponderCodeNode->getIntValue();
184 }
185
186 int CService::getTransponderMode() const
187 {
188 return m_transponderModeNode->getIntValue();
189 }
190
191 bool CService::getTransponderIdent() const
192 {
193 return m_transponderIdentNode->getBoolValue();
194 }
195
196 bool CService::getBeaconLightsOn() const
197 {
198 return m_beaconLightsNode->getBoolValue();
199 }
200
201 bool CService::getLandingLightsOn() const
202 {
203 return m_landingLightsNode->getBoolValue();
204 }
205
206 bool CService::getNavLightsOn() const
207 {
208 return m_navLightsNode->getBoolValue();
209 }
210
211
212 bool CService::getStrobeLightsOn() const
213 {
214 return m_strobeLightsNode->getBoolValue();
215 }
216
217 bool CService::getTaxiLightsOn() const
218 {
219 return m_taxiLightsNode->getBoolValue();
220 }
221
222 double CService::getPressAlt() const
223 {
224 if (m_altimeterServiceableNode->getBoolValue()) {
225 return m_pressAltitudeFtNode->getDoubleValue();
226 } else {
227 return m_altitudeMSLNode->getDoubleValue();
228 }
229 }
230
231 void CService::setCom1Active(int freq)
232 {
233 m_com1ActiveNode->setDoubleValue(freq / (double)1000);
234 }
235
236 void CService::setCom1Standby(int freq)
237 {
238 m_com1StandbyNode->setDoubleValue(freq / (double)1000);
239 }
240
241 void CService::setCom2Active(int freq)
242 {
243 m_com2ActiveNode->setDoubleValue(freq / (double)1000);
244 }
245
246 void CService::setCom2Standby(int freq)
247 {
248 m_com2StandbyNode->setDoubleValue(freq / (double)1000);
249 }
250
251 void CService::setTransponderCode(int code)
252 {
253 m_transponderCodeNode->setIntValue(code);
254 }
255
256 void CService::setTransponderMode(int mode)
257 {
258 m_transponderModeNode->setIntValue(mode);
259 }
260
261 double CService::getFlapsDeployRatio() const
262 {
263 return m_flapsDeployRatioNode->getFloatValue();
264 }
265
266 double CService::getGearDeployRatio() const
267 {
268 return m_gearDeployRatioNode->getFloatValue();
269 }
270
271 int CService::getNumberOfEngines() const
272 {
273 // TODO Use correct property
274 return 2;
275 }
276
277 std::vector<double> CService::getEngineN1Percentage() const
278 {
279 // TODO use correct engine numbers
280 std::vector<double> list;
281 const auto number = static_cast<unsigned int>(getNumberOfEngines());
282 list.reserve(number);
283 for (unsigned int engineNumber = 0; engineNumber < number; ++engineNumber) {
284 list.push_back(fgGetDouble("/engine/engine/n1"));
285 }
286 return list;
287 }
288
289 double CService::getSpeedBrakeRatio() const
290 {
291 return m_speedBrakeDeployRatioNode->getFloatValue();
292 }
293
294 double CService::getGroundElevation() const
295 {
296 return m_groundElevationNode->getDoubleValue();
297 }
298
299 std::string CService::getAircraftModelFilename() const
300 {
301 std::string modelFileName = getAircraftName();
302 modelFileName.append("-set.xml");
303 return modelFileName;
304 }
305
306 std::string CService::getAircraftModelString() const
307 {
308 std::string modelName = getAircraftName();
309 std::string modelString = "FG " + modelName;
310 return modelString;
311 }
312
313 std::string CService::getAircraftName() const
314 {
315 return m_aircraftNameNode->getStringValue();
316 }
317
318 double CService::getVelocityX() const
319 {
320 return m_velocityXNode->getDoubleValue() * SG_FEET_TO_METER;
321 }
322
323 double CService::getVelocityY() const
324 {
325 return m_velocityYNode->getDoubleValue() * SG_FEET_TO_METER * -1; // + (up), - (down)
326 }
327
328 double CService::getVelocityZ() const
329 {
330 return m_velocityZNode->getDoubleValue() * SG_FEET_TO_METER;
331 }
332
333 double CService::getRollRate() const
334 {
335 return m_rollRateNode->getDoubleValue() * SG_DEGREES_TO_RADIANS;
336 }
337 double CService::getPitchRate() const
338 {
339 return m_pichRateNode->getDoubleValue() * SG_DEGREES_TO_RADIANS;
340 }
341
342 double CService::getYawRate() const
343 {
344 return m_yawRateNode->getDoubleValue() * SG_DEGREES_TO_RADIANS;
345 }
346
347 double CService::getCom1Volume() const
348 {
349 return m_com1VolumeNode->getDoubleValue();
350 }
351
352 double CService::getCom2Volume() const
353 {
354 return m_com2VolumeNode->getDoubleValue();
355 }
356
357
358 static const char* introspection_service = DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE;
359
360 DBusHandlerResult CService::dbusMessageHandler(const CDBusMessage& message_)
361 {
362 CDBusMessage message(message_);
363 const std::string sender = message.getSender();
364 const dbus_uint32_t serial = message.getSerial();
365 const bool wantsReply = message.wantsReply();
366
367 if (message.getInterfaceName() == DBUS_INTERFACE_INTROSPECTABLE) {
368 if (message.getMethodName() == "Introspect") {
369 sendDBusReply(sender, serial, introspection_service);
370 }
371 } else if (message.getInterfaceName() == k_fgswiftbus_service_interfacename) {
372 if (message.getMethodName() == "addTextMessage") {
373 maybeSendEmptyDBusReply(wantsReply, sender, serial);
374 std::string text;
375 message.beginArgumentRead();
376 message.getArgument(text);
377
378 queueDBusCall([=]() {
379 addTextMessage(text);
380 });
381 } else if (message.getMethodName() == "getOwnAircraftSituationData") {
382 queueDBusCall([=]() {
383 double lat = getLatitude();
384 double lon = getLongitude();
385 double alt = getAltitudeMSL();
386 double gs = getGroundSpeed();
387 double pitch = getPitch();
388 double roll = getRoll();
389 double trueHeading = getTrueHeading();
390 double pressAlt = getPressAlt();
391 CDBusMessage reply = CDBusMessage::createReply(sender, serial);
392 reply.beginArgumentWrite();
393 reply.appendArgument(lat);
394 reply.appendArgument(lon);
395 reply.appendArgument(alt);
396 reply.appendArgument(gs);
397 reply.appendArgument(pitch);
398 reply.appendArgument(roll);
399 reply.appendArgument(trueHeading);
400 reply.appendArgument(pressAlt);
401 sendDBusMessage(reply);
402 });
403 } else if (message.getMethodName() == "getOwnAircraftVelocityData") {
404 queueDBusCall([=]() {
405 double velocityX = getVelocityX();
406 double velocityY = getVelocityY();
407 double velocityZ = getVelocityZ();
408 double pitchVelocity = getPitchRate();
409 double rollVelocity = getRollRate();
410 double yawVelocity = getYawRate();
411 CDBusMessage reply = CDBusMessage::createReply(sender, serial);
412 reply.beginArgumentWrite();
413 reply.appendArgument(velocityX);
414 reply.appendArgument(velocityY);
415 reply.appendArgument(velocityZ);
416 reply.appendArgument(pitchVelocity);
417 reply.appendArgument(rollVelocity);
418 reply.appendArgument(yawVelocity);
419 sendDBusMessage(reply);
420 });
421 } else if (message.getMethodName() == "getVersionNumber") {
422 queueDBusCall([=]() {
423 sendDBusReply(sender, serial, getVersionNumber());
424 });
425 } else if (message.getMethodName() == "getAircraftModelPath") {
426 queueDBusCall([=]() {
427 sendDBusReply(sender, serial, getAircraftModelPath());
428 });
429 } else if (message.getMethodName() == "getAircraftModelFilename") {
430 queueDBusCall([=]() {
431 sendDBusReply(sender, serial, getAircraftModelFilename());
432 });
433 } else if (message.getMethodName() == "getAircraftModelString") {
434 queueDBusCall([=]() {
435 sendDBusReply(sender, serial, getAircraftModelString());
436 });
437 } else if (message.getMethodName() == "getAircraftName") {
438 queueDBusCall([=]() {
439 sendDBusReply(sender, serial, getAircraftName());
440 });
441 } else if (message.getMethodName() == "getAircraftLivery") {
442 queueDBusCall([=]() {
443 sendDBusReply(sender, serial, getAircraftLivery());
444 });
445 } else if (message.getMethodName() == "getAircraftIcaoCode") {
446 queueDBusCall([=]() {
447 sendDBusReply(sender, serial, getAircraftIcaoCode());
448 });
449 } else if (message.getMethodName() == "getAircraftDescription") {
450 queueDBusCall([=]() {
451 sendDBusReply(sender, serial, getAircraftDescription());
452 });
453 } else if (message.getMethodName() == "isPaused") {
454 queueDBusCall([=]() {
455 sendDBusReply(sender, serial, isPaused());
456 });
457 } else if (message.getMethodName() == "getLatitudeDeg") {
458 queueDBusCall([=]() {
459 sendDBusReply(sender, serial, getLatitude());
460 });
461 } else if (message.getMethodName() == "getLongitudeDeg") {
462 queueDBusCall([=]() {
463 sendDBusReply(sender, serial, getLongitude());
464 });
465 } else if (message.getMethodName() == "getAltitudeMslFt") {
466 queueDBusCall([=]() {
467 sendDBusReply(sender, serial, getAltitudeMSL());
468 });
469 } else if (message.getMethodName() == "getHeightAglFt") {
470 queueDBusCall([=]() {
471 sendDBusReply(sender, serial, getHeightAGL());
472 });
473 } else if (message.getMethodName() == "getGroundSpeedKts") {
474 queueDBusCall([=]() {
475 sendDBusReply(sender, serial, getGroundSpeed());
476 });
477 } else if (message.getMethodName() == "getPitchDeg") {
478 queueDBusCall([=]() {
479 sendDBusReply(sender, serial, getPitch());
480 });
481 } else if (message.getMethodName() == "getRollDeg") {
482 queueDBusCall([=]() {
483 sendDBusReply(sender, serial, getRoll());
484 });
485 } else if (message.getMethodName() == "getAllWheelsOnGround") {
486 queueDBusCall([=]() {
487 sendDBusReply(sender, serial, getAllWheelsOnGround());
488 });
489 } else if (message.getMethodName() == "getCom1ActiveKhz") {
490 queueDBusCall([=]() {
491 sendDBusReply(sender, serial, getCom1Active());
492 });
493 } else if (message.getMethodName() == "getCom1StandbyKhz") {
494 queueDBusCall([=]() {
495 sendDBusReply(sender, serial, getCom1Standby());
496 });
497 } else if (message.getMethodName() == "getCom2ActiveKhz") {
498 queueDBusCall([=]() {
499 sendDBusReply(sender, serial, getCom2Active());
500 });
501 } else if (message.getMethodName() == "getCom2StandbyKhz") {
502 queueDBusCall([=]() {
503 sendDBusReply(sender, serial, getCom2Standby());
504 });
505 } else if (message.getMethodName() == "getTransponderCode") {
506 queueDBusCall([=]() {
507 sendDBusReply(sender, serial, getTransponderCode());
508 });
509 } else if (message.getMethodName() == "getTransponderMode") {
510 queueDBusCall([=]() {
511 sendDBusReply(sender, serial, getTransponderMode());
512 });
513 } else if (message.getMethodName() == "getTransponderIdent") {
514 queueDBusCall([=]() {
515 sendDBusReply(sender, serial, getTransponderIdent());
516 });
517 } else if (message.getMethodName() == "getBeaconLightsOn") {
518 queueDBusCall([=]() {
519 sendDBusReply(sender, serial, getBeaconLightsOn());
520 });
521 } else if (message.getMethodName() == "getLandingLightsOn") {
522 queueDBusCall([=]() {
523 sendDBusReply(sender, serial, getLandingLightsOn());
524 });
525 } else if (message.getMethodName() == "getNavLightsOn") {
526 queueDBusCall([=]() {
527 sendDBusReply(sender, serial, getNavLightsOn());
528 });
529 } else if (message.getMethodName() == "getStrobeLightsOn") {
530 queueDBusCall([=]() {
531 sendDBusReply(sender, serial, getStrobeLightsOn());
532 });
533 } else if (message.getMethodName() == "getTaxiLightsOn") {
534 queueDBusCall([=]() {
535 sendDBusReply(sender, serial, getTaxiLightsOn());
536 });
537 } else if (message.getMethodName() == "getPressAlt") {
538 queueDBusCall([=]() {
539 sendDBusReply(sender, serial, getPressAlt());
540 });
541 } else if (message.getMethodName() == "getGroundElevation") {
542 queueDBusCall([=]() {
543 sendDBusReply(sender, serial, getGroundElevation());
544 });
545 } else if (message.getMethodName() == "setCom1ActiveKhz") {
546 maybeSendEmptyDBusReply(wantsReply, sender, serial);
547 int frequency = 0;
548 message.beginArgumentRead();
549 message.getArgument(frequency);
550 queueDBusCall([=]() {
551 setCom1Active(frequency);
552 });
553 } else if (message.getMethodName() == "setCom1StandbyKhz") {
554 maybeSendEmptyDBusReply(wantsReply, sender, serial);
555 int frequency = 0;
556 message.beginArgumentRead();
557 message.getArgument(frequency);
558 queueDBusCall([=]() {
559 setCom1Standby(frequency);
560 });
561 } else if (message.getMethodName() == "setCom2ActiveKhz") {
562 maybeSendEmptyDBusReply(wantsReply, sender, serial);
563 int frequency = 0;
564 message.beginArgumentRead();
565 message.getArgument(frequency);
566 queueDBusCall([=]() {
567 setCom2Active(frequency);
568 });
569 } else if (message.getMethodName() == "setCom2StandbyKhz") {
570 maybeSendEmptyDBusReply(wantsReply, sender, serial);
571 int frequency = 0;
572 message.beginArgumentRead();
573 message.getArgument(frequency);
574 queueDBusCall([=]() {
575 setCom2Standby(frequency);
576 });
577 } else if (message.getMethodName() == "setTransponderCode") {
578 maybeSendEmptyDBusReply(wantsReply, sender, serial);
579 int code = 0;
580 message.beginArgumentRead();
581 message.getArgument(code);
582 queueDBusCall([=]() {
583 setTransponderCode(code);
584 });
585 } else if (message.getMethodName() == "setTransponderMode") {
586 maybeSendEmptyDBusReply(wantsReply, sender, serial);
587 int mode = 0;
588 message.beginArgumentRead();
589 message.getArgument(mode);
590 queueDBusCall([=]() {
591 setTransponderMode(mode);
592 });
593 } else if (message.getMethodName() == "getFlapsDeployRatio") {
594 queueDBusCall([=]() {
595 sendDBusReply(sender, serial, getFlapsDeployRatio());
596 });
597 } else if (message.getMethodName() == "getGearDeployRatio") {
598 queueDBusCall([=]() {
599 sendDBusReply(sender, serial, getGearDeployRatio());
600 });
601 } else if (message.getMethodName() == "getEngineN1Percentage") {
602 queueDBusCall([=]() {
603 std::vector<double> array = getEngineN1Percentage();
604 sendDBusReply(sender, serial, array);
605 });
606 } else if (message.getMethodName() == "getSpeedBrakeRatio") {
607 queueDBusCall([=]() {
608 sendDBusReply(sender, serial, getSpeedBrakeRatio());
609 });
610 } else if (message.getMethodName() == "getCom1Volume") {
611 queueDBusCall([=]() {
612 sendDBusReply(sender, serial, getCom1Volume());
613 });
614 } else if (message.getMethodName() == "getCom2Volume") {
615 queueDBusCall([=]() {
616 sendDBusReply(sender, serial, getCom2Volume());
617 });
618 } else {
619 // Unknown message. Tell DBus that we cannot handle it
620 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
621 }
622 }
623 return DBUS_HANDLER_RESULT_HANDLED;
624 }
625
626
627 int CService::process()
628 {
629 invokeQueuedDBusCalls();
630
631 return 1;
632 }
633
634 } // namespace flightgear::swift