mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 05:26:45 +08:00
Previously, the DBusconnection had to be set at construction time and could not be changed over the DBusObject life time. Now a new connection can be set anytime and as often as required. The shared ownership is now also properly implemented by shared pointers. ref T291
91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
/* Copyright (C) 2018
|
|
* swift project Community / Contributors
|
|
*
|
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
|
* contained in the LICENSE file.
|
|
*/
|
|
|
|
#include "dbusobject.h"
|
|
#include <cassert>
|
|
|
|
namespace XSwiftBus
|
|
{
|
|
CDBusObject::CDBusObject()
|
|
{ }
|
|
|
|
CDBusObject::~CDBusObject() = default;
|
|
|
|
void CDBusObject::setDBusConnection(const std::shared_ptr<CDBusConnection> &dbusConnection)
|
|
{
|
|
m_dbusConnection = dbusConnection;
|
|
}
|
|
|
|
void CDBusObject::registerDBusObjectPath(const std::string &interfaceName, const std::string &objectPath)
|
|
{
|
|
assert(m_dbusConnection);
|
|
m_interfaceName = interfaceName;
|
|
m_objectPath = objectPath;
|
|
m_dbusConnection->registerObjectPath(this, interfaceName, objectPath, m_dbusObjectPathVTable);
|
|
}
|
|
|
|
void CDBusObject::sendDBusSignal(const std::string &name)
|
|
{
|
|
if (! m_dbusConnection) { return; }
|
|
CDBusMessage signal = CDBusMessage::createSignal(m_objectPath, m_interfaceName, name);
|
|
m_dbusConnection->sendMessage(signal);
|
|
}
|
|
|
|
void CDBusObject::sendDBusMessage(const CDBusMessage &message)
|
|
{
|
|
if (! m_dbusConnection) { return; }
|
|
m_dbusConnection->sendMessage(message);
|
|
}
|
|
|
|
void CDBusObject::maybeSendEmptyDBusReply(bool wantsReply, const std::string &destination, dbus_uint32_t serial)
|
|
{
|
|
if (wantsReply)
|
|
{
|
|
CDBusMessage reply = CDBusMessage::createReply(destination, serial);
|
|
m_dbusConnection->sendMessage(reply);
|
|
}
|
|
}
|
|
|
|
void CDBusObject::queueDBusCall(const std::function<void ()> &func)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_qeuedDBusCalls.push_back(func);
|
|
}
|
|
|
|
void CDBusObject::invokeQueuedDBusCalls()
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
while (m_qeuedDBusCalls.size() > 0)
|
|
{
|
|
m_qeuedDBusCalls.front()();
|
|
m_qeuedDBusCalls.pop_front();
|
|
}
|
|
}
|
|
|
|
void CDBusObject::dbusObjectPathUnregisterFunction(DBusConnection *connection, void *data)
|
|
{
|
|
(void)connection; // unused
|
|
(void)data; // unused
|
|
}
|
|
|
|
DBusHandlerResult CDBusObject::dbusObjectPathMessageFunction(DBusConnection *connection, DBusMessage *message, void *data)
|
|
{
|
|
(void)connection; // unused
|
|
|
|
auto *obj = static_cast<CDBusObject *>(data);
|
|
|
|
DBusError err;
|
|
dbus_error_init(&err);
|
|
|
|
CDBusMessage dbusMessage(message);
|
|
return obj->dbusMessageHandler(dbusMessage);
|
|
}
|
|
|
|
}
|