#include "GattCharacteristic.h"
#include "GattDescriptor.h"
#include "GattProperty.h"
#include "GattUuid.h"
#include "DBusObject.h"
#include "GattService.h"
#include "Utils.h"
#include "Logger.h"
namespace ggk {
GattCharacteristic::GattCharacteristic(DBusObject &owner, GattService &service, const std::string &name)
: GattInterface(owner, name), service(service), pOnUpdatedValueFunc(nullptr)
{
}
GattService &GattCharacteristic::gattCharacteristicEnd()
{
return service;
}
bool GattCharacteristic::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<GattCharacteristic>(pConnection, getPath(), getName(), methodName, pParameters, pInvocation, pUserData);
return true;
}
}
return false;
}
GattCharacteristic &GattCharacteristic::onEvent(int tickFrequency, void *pUserData, EventCallback callback)
{
events.push_back(TickEvent(this, tickFrequency, reinterpret_cast<TickEvent::Callback>(callback), pUserData));
return *this;
}
void GattCharacteristic::tickEvents(GDBusConnection *pConnection, void *pUserData) const
{
for (const TickEvent &event : events)
{
event.tick<GattCharacteristic>(getPath(), pConnection, pUserData);
}
}
GattCharacteristic &GattCharacteristic::onReadValue(MethodCallback callback)
{
static const char *inArgs[] = {"a{sv}", nullptr};
addMethod("ReadValue", inArgs, "ay", reinterpret_cast<DBusMethod::Callback>(callback));
return *this;
}
GattCharacteristic &GattCharacteristic::onWriteValue(MethodCallback callback)
{
static const char *inArgs[] = {"ay", "a{sv}", nullptr};
addMethod("WriteValue", inArgs, nullptr, reinterpret_cast<DBusMethod::Callback>(callback));
return *this;
}
GattCharacteristic &GattCharacteristic::onUpdatedValue(UpdatedValueCallback callback)
{
pOnUpdatedValueFunc = callback;
return *this;
}
bool GattCharacteristic::callOnUpdatedValue(GDBusConnection *pConnection, void *pUserData) const
{
if (nullptr == pOnUpdatedValueFunc)
{
return false;
}
Logger::trace(SSTR << "Calling OnUpdatedValue function for interface at path '" << getPath() << "'");
return pOnUpdatedValueFunc(*this, pConnection, pUserData);
}
GattDescriptor &GattCharacteristic::gattDescriptorBegin(const std::string &pathElement, const GattUuid &uuid, const std::vector<const char *> &flags)
{
DBusObject &child = owner.addChild(DBusObjectPath(pathElement));
GattDescriptor &descriptor = *child.addInterface(std::make_shared<GattDescriptor>(child, *this, "org.bluez.GattDescriptor1"));
descriptor.addProperty<GattDescriptor>("UUID", uuid);
descriptor.addProperty<GattDescriptor>("Characteristic", getPath());
descriptor.addProperty<GattDescriptor>("Flags", flags);
return descriptor;
}
void GattCharacteristic::sendChangeNotificationVariant(GDBusConnection *pBusConnection, GVariant *pNewValue) const
{
g_auto(GVariantBuilder) builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
g_variant_builder_add(&builder, "{sv}", "Value", pNewValue);
GVariant *pSasv = g_variant_new("(sa{sv})", "org.bluez.GattCharacteristic1", &builder);
owner.emitSignal(pBusConnection, "org.freedesktop.DBus.Properties", "PropertiesChanged", pSasv);
}
};