21 m_objectPath(QString(appObjectPathTemplate).
22 arg(sanitizeNameForDBus(QCoreApplication::applicationName())).
23 arg(QCoreApplication::applicationPid()).
24 arg(QRandomGenerator::global()->generate()))
26 m_objectManager =
new OrgFreedesktopDBusObjectManagerAdaptor(
this);
27 m_gattManager =
new OrgBluezGattManager1Interface(
"org.bluez"_L1, hostAdapterPath,
28 QDBusConnection::systemBus(),
this);
38 if (m_applicationRegistered) {
40 qCDebug(QT_BT_BLUEZ) <<
"Bluez peripheral application already registered";
44 if (m_services.isEmpty()) {
46 qCDebug(QT_BT_BLUEZ) <<
"No services, omiting Bluez peripheral application registration";
50 qCDebug(QT_BT_BLUEZ) <<
"Registering bluez peripheral application:" << m_objectPath;
53 if (!QDBusConnection::systemBus().registerObject(m_objectPath, m_objectManager,
54 QDBusConnection::ExportAllContents)) {
55 qCWarning(QT_BT_BLUEZ) <<
"Peripheral application object registration failed";
66 auto reply = m_gattManager->RegisterApplication(QDBusObjectPath(m_objectPath), {});
67 QDBusPendingCallWatcher* watcher =
new QDBusPendingCallWatcher(reply,
this);
68 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
this,
69 [
this](QDBusPendingCallWatcher* watcher) {
70 QDBusPendingReply<> reply = *watcher;
71 if (reply.isError()) {
72 qCWarning(QT_BT_BLUEZ) <<
"Application registration failed" << reply.error();
73 QDBusConnection::systemBus().unregisterObject(m_objectPath);
76 qCDebug(QT_BT_BLUEZ) <<
"Peripheral application registered as" << m_objectPath;
77 m_applicationRegistered =
true;
80 watcher->deleteLater();
136 QSharedPointer<QLowEnergyServicePrivate> servicePrivate,
137 QLowEnergyHandle serviceHandle)
139 if (m_applicationRegistered) {
140 qCWarning(QT_BT_BLUEZ) <<
"Adding services to a registered application is not supported "
141 "on Bluez DBus. Add services only before first advertisement or "
142 "after disconnection";
153 serviceData, m_objectPath, m_services.size(), serviceHandle,
this);
154 m_services.insert(serviceHandle, service);
157 const auto includedServices = serviceData.includedServices();
158 for (
const auto includedService : includedServices) {
160 for (
const auto s : std::as_const(m_services)) {
161 if (QBluetoothUuid(s->uuid) == includedService->serviceUuid()) {
162 service->addIncludedService(s->objectPath);
168 quint16 characteristicOrdinal{0};
169 const auto characteristics = serviceData.characteristics();
170 for (
const auto &characteristicData : characteristics) {
171 auto characteristicHandle = handleForCharacteristic(
172 characteristicData.uuid(), servicePrivate);
173 QtBluezPeripheralCharacteristic* characteristic =
174 new QtBluezPeripheralCharacteristic(characteristicData,
175 service->objectPath, characteristicOrdinal++,
176 characteristicHandle,
this);
177 m_characteristics.insert(characteristicHandle, characteristic);
178 QObject::connect(characteristic, &QtBluezPeripheralCharacteristic::valueUpdatedByRemote,
179 this, &QtBluezPeripheralApplication::characteristicValueUpdatedByRemote);
180 QObject::connect(characteristic, &QtBluezPeripheralCharacteristic::remoteDeviceAccessEvent,
181 this, &QtBluezPeripheralApplication::remoteDeviceAccessEvent);
183 quint16 descriptorOrdinal{0};
184 const auto descriptors = characteristicData.descriptors();
185 for (
const auto &descriptorData : descriptors) {
190 if (descriptorData.uuid()
191 == QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration
192 || descriptorData.uuid()
193 == QBluetoothUuid::DescriptorType::CharacteristicExtendedProperties) {
196 auto descriptorHandle = handleForDescriptor(descriptorData.uuid(),
198 characteristicHandle);
199 QtBluezPeripheralDescriptor* descriptor =
200 new QtBluezPeripheralDescriptor(descriptorData,
201 characteristic->objectPath, descriptorOrdinal++,
202 descriptorHandle, characteristicHandle,
this);
203 QObject::connect(descriptor, &QtBluezPeripheralDescriptor::valueUpdatedByRemote,
204 this, &QtBluezPeripheralApplication::descriptorValueUpdatedByRemote);
205 QObject::connect(descriptor, &QtBluezPeripheralCharacteristic::remoteDeviceAccessEvent,
206 this, &QtBluezPeripheralApplication::remoteDeviceAccessEvent);
207 m_descriptors.insert(descriptorHandle, descriptor);
245 ManagedObjectList managedObjects;
246 for (
const auto service: std::as_const(m_services))
247 managedObjects.insert(QDBusObjectPath(service->objectPath), service->properties());
248 for (
const auto& charac : std::as_const(m_characteristics))
249 managedObjects.insert(QDBusObjectPath(charac->objectPath), charac->properties());
250 for (
const auto& descriptor : std::as_const(m_descriptors))
251 managedObjects.insert(QDBusObjectPath(descriptor->objectPath), descriptor->properties());
253 return managedObjects;