#pragma once
#include <gio/gio.h>
#include <string>
#include <list>
#include "TickEvent.h"
#include "DBusInterface.h"
#include "GattProperty.h"
#include "GattUuid.h"
#include "Server.h"
#include "Utils.h"
namespace ggk {
struct GattInterface;
struct DBusObject;
struct GattInterface : DBusInterface
{
GattInterface(DBusObject &owner, const std::string &name);
virtual ~GattInterface();
virtual const std::string getInterfaceType() const = 0;
const std::list<GattProperty> &getProperties() const;
template<typename T>
T &addProperty(const GattProperty &property)
{
properties.push_back(property);
return *static_cast<T *>(this);
}
template<typename T>
T &addProperty(const std::string &name, GVariant *pValue, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, pValue, getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const GattUuid &uuid, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromString(uuid.toString128().c_str()), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const DBusObjectPath &path, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromObject(path), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const std::string &str, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromString(str), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const std::vector<std::string> &arr, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromStringArray(arr), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const std::vector<const char *> &arr, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromStringArray(arr), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, const char *pStr, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromString(pStr), getter, setter));
}
template<typename T>
T &addProperty(const std::string &name, bool value, GDBusInterfaceGetPropertyFunc getter = nullptr, GDBusInterfaceSetPropertyFunc setter = nullptr)
{
return addProperty<T>(GattProperty(name, Utils::gvariantFromBoolean(value), getter, setter));
}
template<typename T>
T getDataValue(const char *pName, const T defaultValue) const
{
const void *pData = TheServer->getDataGetter()(pName);
return nullptr == pData ? defaultValue : *static_cast<const T *>(pData);
}
template<typename T>
T getDataPointer(const char *pName, const T defaultValue) const
{
const void *pData = TheServer->getDataGetter()(pName);
return nullptr == pData ? defaultValue : static_cast<const T>(pData);
}
template<typename T>
bool setDataValue(const char *pName, const T value) const
{
return TheServer->getDataSetter()(pName, static_cast<const void *>(&value)) != 0;
}
template<typename T>
bool setDataPointer(const char *pName, const T pointer) const
{
return TheServer->getDataSetter()(pName, static_cast<const void *>(pointer)) != 0;
}
void methodReturnVariant(GDBusMethodInvocation *pInvocation, GVariant *pVariant, bool wrapInTuple = false) const;
template<typename T>
void methodReturnValue(GDBusMethodInvocation *pInvocation, T value, bool wrapInTuple = false) const
{
GVariant *pVariant = Utils::gvariantFromByteArray(value);
methodReturnVariant(pInvocation, pVariant, wrapInTuple);
}
const GattProperty *findProperty(const std::string &name) const;
virtual std::string generateIntrospectionXML(int depth) const;
protected:
std::list<GattProperty> properties;
};
};