#include "simplebluez/interfaces/GattCharacteristic1.h"
using namespace SimpleBluez;
const SimpleDBus::AutoRegisterInterface<GattCharacteristic1> GattCharacteristic1::registry{
"org.bluez.GattCharacteristic1",
[](std::shared_ptr<SimpleDBus::Connection> conn, std::shared_ptr<SimpleDBus::Proxy> proxy) -> std::shared_ptr<SimpleDBus::Interface> {
return std::static_pointer_cast<SimpleDBus::Interface>(std::make_shared<GattCharacteristic1>(conn, proxy));
}
};
GattCharacteristic1::GattCharacteristic1(std::shared_ptr<SimpleDBus::Connection> conn,
std::shared_ptr<SimpleDBus::Proxy> proxy)
: SimpleDBus::Interface(conn, proxy, "org.bluez.GattCharacteristic1") {}
GattCharacteristic1::~GattCharacteristic1() = default;
void GattCharacteristic1::StartNotify() {
auto msg = create_method_call("StartNotify");
_conn->send_with_reply_and_block(msg);
}
void GattCharacteristic1::StopNotify() {
auto msg = create_method_call("StopNotify");
_conn->send_with_reply_and_block(msg);
}
void GattCharacteristic1::WriteValue(const ByteArray& value, WriteType type) {
SimpleDBus::Holder value_data = SimpleDBus::Holder::create_array();
for (size_t i = 0; i < value.size(); i++) {
value_data.array_append(SimpleDBus::Holder::create_byte(value[i]));
}
SimpleDBus::Holder options = SimpleDBus::Holder::create_dict();
if (type == WriteType::REQUEST) {
options.dict_append(SimpleDBus::Holder::Type::STRING, "type", SimpleDBus::Holder::create_string("request"));
} else if (type == WriteType::COMMAND) {
options.dict_append(SimpleDBus::Holder::Type::STRING, "type", SimpleDBus::Holder::create_string("command"));
}
auto msg = create_method_call("WriteValue");
msg.append_argument(value_data, "ay");
msg.append_argument(options, "a{sv}");
_conn->send_with_reply_and_block(msg);
}
ByteArray GattCharacteristic1::ReadValue() {
auto msg = create_method_call("ReadValue");
SimpleDBus::Holder options = SimpleDBus::Holder::create_dict();
msg.append_argument(options, "a{sv}");
SimpleDBus::Message reply_msg = _conn->send_with_reply_and_block(msg);
SimpleDBus::Holder value = reply_msg.extract();
Value.set(value);
return Value();
}