#include "DBusInterface.h"
#include "GattProperty.h"
#include "DBusObject.h"
#include "Logger.h"
DBusInterface::DBusInterface(DBusObject &owner, const std::string &name)
: owner(owner), name(name)
{
}
DBusInterface::~DBusInterface()
{
}
const std::string &DBusInterface::getName() const
{
return name;
}
DBusInterface &DBusInterface::setName(const std::string &name)
{
this->name = name;
return *this;
}
DBusObject &DBusInterface::getOwner() const
{
return owner;
}
DBusObjectPath DBusInterface::getPathNode() const
{
return owner.getPathNode();
}
DBusObjectPath DBusInterface::getPath() const
{
return owner.getPath();
}
DBusInterface &DBusInterface::addMethod(const std::string &name, const char *pInArgs[], const char *pOutArgs, DBusMethod::Callback callback)
{
methods.push_back(DBusMethod(this, name, pInArgs, pOutArgs, callback));
return *this;
}
bool DBusInterface::callMethod(const std::string &methodName, GDBusConnection *pConnection, GVariant *pParameters, GDBusMethodInvocation *pInvocation, gpointer pUserData) const
{
for (const DBusMethod &method : methods)
{
if (methodName == method.getName())
{
method.call<DBusInterface>(pConnection, getPath(), getName(), methodName, pParameters, pInvocation, pUserData);
return true;
}
}
return false;
}
DBusInterface &DBusInterface::onEvent(int tickFrequency, void *pUserData, TickEvent::Callback callback)
{
events.push_back(TickEvent(this, tickFrequency, callback, pUserData));
return *this;
}
void DBusInterface::tickEvents(GDBusConnection *pConnection, void *pUserData) const
{
for (const TickEvent &event : events)
{
event.tick<DBusInterface>(getPath(), pConnection, pUserData);
}
}
std::string DBusInterface::generateIntrospectionXML(int depth) const
{
std::string prefix;
prefix.insert(0, depth * 2, ' ');
std::string xml = std::string();
if (methods.empty())
{
xml += prefix + "<interface name='" + getName() + "' />\n";
}
else
{
xml += prefix + "<interface name='" + getName() + "'>\n";
for (const DBusMethod &method : methods)
{
xml += method.generateIntrospectionXML(depth + 1);
}
xml += prefix + "</interface>\n";
}
return xml;
}