feat(xswiftbus): Expose callsign as dataref

Fixes #379
This commit is contained in:
Lars Toenning
2025-12-12 20:28:04 +01:00
parent a71b8e3491
commit 2cb18c5b90
12 changed files with 90 additions and 9 deletions

View File

@@ -802,12 +802,15 @@ namespace swift::core::context
{ {
Q_UNUSED(from) Q_UNUSED(from)
SWIFT_VERIFY_X(this->getIContextNetwork(), Q_FUNC_INFO, "Missing network context"); SWIFT_VERIFY_X(this->getIContextNetwork(), Q_FUNC_INFO, "Missing network context");
SWIFT_VERIFY_X(this->getIContextOwnAircraft(), Q_FUNC_INFO, "Missing own aircraft context");
if (to.isConnected() && this->getIContextNetwork()) if (to.isConnected() && this->getIContextNetwork())
{ {
m_networkSessionId = this->getIContextNetwork()->getConnectedServer().getServerSessionId(false); m_networkSessionId = this->getIContextNetwork()->getConnectedServer().getServerSessionId(false);
if (m_simulatorPlugin.second) // check in case the plugin has been unloaded if (m_simulatorPlugin.second) // check in case the plugin has been unloaded
{ {
m_simulatorPlugin.second->setFlightNetworkConnected(true); m_simulatorPlugin.second->setFlightNetworkConnected(true);
m_simulatorPlugin.second->setOwnCallsign(
this->getIContextOwnAircraft()->getOwnAircraft().getCallsign());
} }
} }
else if (to.isDisconnected()) else if (to.isDisconnected())
@@ -821,6 +824,7 @@ namespace swift::core::context
{ {
m_simulatorPlugin.second->removeAllRemoteAircraft(); // also removes aircraft m_simulatorPlugin.second->removeAllRemoteAircraft(); // also removes aircraft
m_simulatorPlugin.second->setFlightNetworkConnected(false); m_simulatorPlugin.second->setFlightNetworkConnected(false);
m_simulatorPlugin.second->setOwnCallsign({});
} }
} }
} }

View File

@@ -119,6 +119,12 @@ namespace swift::core
void ISimulator::setFlightNetworkConnected(bool connected) { m_networkConnected = connected; } void ISimulator::setFlightNetworkConnected(bool connected) { m_networkConnected = connected; }
void ISimulator::setOwnCallsign(const CCallsign &callsign)
{
Q_UNUSED(callsign)
// void
}
void ISimulator::clearAllRemoteAircraftData() void ISimulator::clearAllRemoteAircraftData()
{ {
// rendering related stuff // rendering related stuff

View File

@@ -152,6 +152,9 @@ namespace swift::core
//! \sa ISimulator::isFlightNetworkConnected //! \sa ISimulator::isFlightNetworkConnected
virtual void setFlightNetworkConnected(bool connected); virtual void setFlightNetworkConnected(bool connected);
//! Own callsign has changed
virtual void setOwnCallsign(const swift::misc::aviation::CCallsign &callsign);
//! Is the flight network connected //! Is the flight network connected
bool isFlightNetworkConnected() const { return m_networkConnected; } bool isFlightNetworkConnected() const { return m_networkConnected; }

View File

@@ -219,6 +219,12 @@ namespace swift::simplugin::xplane
CSimulatorPluginCommon::setFlightNetworkConnected(connected); CSimulatorPluginCommon::setFlightNetworkConnected(connected);
} }
void CSimulatorXPlane::setOwnCallsign(const CCallsign &callsign)
{
m_serviceProxy->setOwnCallsign(callsign.asString());
CSimulatorPluginCommon::setOwnCallsign(callsign);
}
bool CSimulatorXPlane::isSuspiciousTerrainValue(const CElevationPlane &elevation) bool CSimulatorXPlane::isSuspiciousTerrainValue(const CElevationPlane &elevation)
{ {
if (!elevation.hasMSLGeodeticHeight()) { return true; } if (!elevation.hasMSLGeodeticHeight()) { return true; }

View File

@@ -150,6 +150,7 @@ namespace swift::simplugin::xplane
const swift::misc::aviation::CCallsign &callsign, const swift::misc::aviation::CCallsign &callsign,
bool isWater) override; bool isWater) override;
void setFlightNetworkConnected(bool connected) override; void setFlightNetworkConnected(bool connected) override;
void setOwnCallsign(const swift::misc::aviation::CCallsign &callsign) override;
//! @} //! @}
//! \copydoc swift::misc::simulation::ISimulationEnvironmentProvider::requestElevation //! \copydoc swift::misc::simulation::ISimulationEnvironmentProvider::requestElevation

View File

@@ -363,6 +363,11 @@ namespace swift::simplugin::xplane
m_dbusInterface->callDBus(QLatin1String("setFlightNetworkConnected"), connected); m_dbusInterface->callDBus(QLatin1String("setFlightNetworkConnected"), connected);
} }
void CXSwiftBusServiceProxy::setOwnCallsign(const QString &callsign)
{
m_dbusInterface->callDBus(QLatin1String("setOwnCallsign"), callsign);
}
double CXSwiftBusServiceProxy::getLatitudeDeg() const double CXSwiftBusServiceProxy::getLatitudeDeg() const
{ {
return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg")); return m_dbusInterface->callDBusRet<double>(QLatin1String("getLatitudeDeg"));

View File

@@ -215,6 +215,9 @@ namespace swift::simplugin::xplane
//! \copydoc XSwiftBus::CService::setFlightNetworkConnected //! \copydoc XSwiftBus::CService::setFlightNetworkConnected
void setFlightNetworkConnected(bool connected); void setFlightNetworkConnected(bool connected);
//! \copydoc XSwiftBus::CService::setOwnCallsign
void setOwnCallsign(const QString &callsign);
//! @{ //! @{
//! \copydoc XSwiftBus::CService::getLatitudeDeg //! \copydoc XSwiftBus::CService::getLatitudeDeg
double getLatitudeDeg() const; double getLatitudeDeg() const;

View File

@@ -11,12 +11,17 @@ namespace XSwiftBus
{ {
//! Dataref name //! Dataref name
static constexpr const char *name() { return "org/swift-project/xswiftbus/connected"; } static constexpr const char *name() { return "org/swift-project/xswiftbus/connected"; }
//! Can be written to?
static constexpr bool writable = false;
//! Dataref type //! Dataref type
using type = int; using type = int;
//! Not an array dataref };
static constexpr bool is_array = false;
//! Current callsign
struct TSwiftCallsign
{
//! Dataref name
static constexpr const char *name() { return "org/swift-project/xswiftbus/callsign"; }
//! Dataref type
using type = std::string;
}; };
} // namespace XSwiftBus } // namespace XSwiftBus

View File

@@ -9,8 +9,10 @@
#include <XPLM/XPLMDataAccess.h> #include <XPLM/XPLMDataAccess.h>
#include <XPLM/XPLMUtilities.h> #include <XPLM/XPLMUtilities.h>
#include <algorithm>
#include <array> #include <array>
#include <cassert> #include <cassert>
#include <cstring>
#include <string> #include <string>
// Avoid checking large auto-generated header with cppcheck // Avoid checking large auto-generated header with cppcheck
@@ -223,8 +225,13 @@ namespace XSwiftBus
XPLMDataRef m_ref; XPLMDataRef m_ref;
}; };
/* Helper to conditionally fail compilation if no matching constexpr if case is found */
template <class...>
constexpr bool dependent_false = false;
/*! /*!
* Class providing a custom variable + dataref * Class providing a custom variable + dataref
* \hint Currently only readable int and std::string datarefs are supported
* \tparam DataRefTraits The trait class representing the dataref. * \tparam DataRefTraits The trait class representing the dataref.
*/ */
template <class DataRefTraits> template <class DataRefTraits>
@@ -236,10 +243,17 @@ namespace XSwiftBus
{ {
if constexpr (std::is_same_v<typename DataRefTraits::type, int>) if constexpr (std::is_same_v<typename DataRefTraits::type, int>)
{ {
m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Int, 0, read, NULL, NULL, NULL, NULL, m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Int, 0, readInt, nullptr, nullptr,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, this, NULL); nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, this, nullptr);
} }
else { XPLMDebugString("Unsupported custom dataref type\n"); } else if constexpr (std::is_same_v<typename DataRefTraits::type, std::string>)
{
m_ref = XPLMRegisterDataAccessor(DataRefTraits::name(), xplmType_Data, 0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
readData, nullptr, this, nullptr);
}
else { static_assert(dependent_false<typename DataRefTraits::type>, "Unsupported custom dataref type"); }
if (!m_ref) if (!m_ref)
{ {
XPLMDebugString("Missing dataref:"); XPLMDebugString("Missing dataref:");
@@ -257,11 +271,27 @@ namespace XSwiftBus
if (m_ref) { XPLMUnregisterDataAccessor(m_ref); } if (m_ref) { XPLMUnregisterDataAccessor(m_ref); }
} }
static typename DataRefTraits::type read(void *refcon) static typename DataRefTraits::type readInt(void *refcon)
{ {
return reinterpret_cast<CustomDataRef *>(refcon)->get(); return reinterpret_cast<CustomDataRef *>(refcon)->get();
} }
static int readData(void *refcon, void *out, int offset, int max_length)
{
if constexpr (std::is_same_v<typename DataRefTraits::type, std::string>)
{
std::string_view sv(reinterpret_cast<CustomDataRef *>(refcon)->get());
const size_t start = std::clamp<size_t>(offset, 0, sv.size());
const size_t remaining_length = sv.size() - start;
const auto len = std::min(static_cast<size_t>(max_length), remaining_length);
sv = sv.substr(start, len);
std::memcpy(out, sv.data(), len);
return static_cast<int>(len);
}
return -1; // should never be reached
}
//! True if the dataref exists //! True if the dataref exists
bool isValid() const { return m_ref != nullptr; } bool isValid() const { return m_ref != nullptr; }
@@ -269,7 +299,7 @@ namespace XSwiftBus
void set(typename DataRefTraits::type val) { m_datarefVal = val; } void set(typename DataRefTraits::type val) { m_datarefVal = val; }
//! Get the value //! Get the value
typename DataRefTraits::type get() const { return m_datarefVal; } const typename DataRefTraits::type &get() const { return m_datarefVal; }
XPLMDataRef m_ref; XPLMDataRef m_ref;
typename DataRefTraits::type m_datarefVal; typename DataRefTraits::type m_datarefVal;

View File

@@ -116,6 +116,9 @@ R"XML(<node>
<method name="setFlightNetworkConnected"> <method name="setFlightNetworkConnected">
<arg type="b" direction="in"/> <arg type="b" direction="in"/>
</method> </method>
<method name="setOwnCallsign">
<arg type="s" direction="in"/>
</method>
<method name="getLatitudeDeg"> <method name="getLatitudeDeg">
<arg type="d" direction="out"/> <arg type="d" direction="out"/>
</method> </method>

View File

@@ -94,6 +94,7 @@ namespace XSwiftBus
this->updateMessageBoxFromSettings(); this->updateMessageBoxFromSettings();
m_framePeriodSampler->show(); m_framePeriodSampler->show();
m_swiftNetworkConnected.set(0); m_swiftNetworkConnected.set(0);
m_swiftCallsign.set("");
} }
// Explicitly in cpp file to allow use of forward declaration // Explicitly in cpp file to allow use of forward declaration
@@ -139,6 +140,8 @@ namespace XSwiftBus
void CService::setFlightNetworkConnected(bool connected) { m_swiftNetworkConnected.set(connected); } void CService::setFlightNetworkConnected(bool connected) { m_swiftNetworkConnected.set(connected); }
void CService::setOwnCallsign(const std::string &callsign) { m_swiftCallsign.set(callsign); }
void CService::addTextMessage(const std::string &text, double red, double green, double blue) void CService::addTextMessage(const std::string &text, double red, double green, double blue)
{ {
if (text.empty()) { return; } if (text.empty()) { return; }
@@ -520,6 +523,14 @@ namespace XSwiftBus
message.getArgument(connected); message.getArgument(connected);
queueDBusCall([=]() { setFlightNetworkConnected(connected); }); queueDBusCall([=]() { setFlightNetworkConnected(connected); });
} }
else if (message.getMethodName() == "setOwnCallsign")
{
maybeSendEmptyDBusReply(wantsReply, sender, serial);
std::string callsign;
message.beginArgumentRead();
message.getArgument(callsign);
queueDBusCall([=]() { setOwnCallsign(callsign); });
}
else if (message.getMethodName() == "getLatitudeDeg") else if (message.getMethodName() == "getLatitudeDeg")
{ {
queueDBusCall([=]() { sendDBusReply(sender, serial, getLatitudeDeg()); }); queueDBusCall([=]() { sendDBusReply(sender, serial, getLatitudeDeg()); });

View File

@@ -123,6 +123,9 @@ namespace XSwiftBus
//! Set the current connection state //! Set the current connection state
void setFlightNetworkConnected(bool connected); void setFlightNetworkConnected(bool connected);
//! Set the current own callsign
void setOwnCallsign(const std::string &callsign);
//! Get aircraft latitude in degrees //! Get aircraft latitude in degrees
double getLatitudeDeg() const { return m_latitude.get(); } double getLatitudeDeg() const { return m_latitude.get(); }
@@ -354,6 +357,7 @@ namespace XSwiftBus
int m_sceneryWasLoading = 0; int m_sceneryWasLoading = 0;
CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected; CustomDataRef<TSwiftNetworkConnected> m_swiftNetworkConnected;
CustomDataRef<TSwiftCallsign> m_swiftCallsign;
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath; StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao; StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip; StringDataRef<xplane::data::sim::aircraft::view::acf_descrip> m_descrip;