summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergycontroller_bluezdbus_p.h
blob: afa2e8f32f9aed1a0a1feec9df211e85bf6aa70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QLOWENERGYCONTROLLERPRIVATEDBUS_P_H
#define QLOWENERGYCONTROLLERPRIVATEDBUS_P_H


//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qlowenergycontroller.h"
#include "qlowenergycontrollerbase_p.h"
#include "qleadvertiser_bluezdbus_p.h"

#include <QtDBus/QDBusObjectPath>

class OrgBluezAdapter1Interface;
class OrgBluezBattery1Interface;
class OrgBluezDevice1Interface;
class OrgBluezGattCharacteristic1Interface;
class OrgBluezGattDescriptor1Interface;
class OrgBluezGattService1Interface;
class OrgFreedesktopDBusObjectManagerInterface;
class OrgFreedesktopDBusPropertiesInterface;

QT_BEGIN_NAMESPACE

class QtBluezPeripheralApplication;
class QtBluezPeripheralConnectionManager;
class QDBusPendingCallWatcher;

class QLowEnergyControllerPrivateBluezDBus final : public QLowEnergyControllerPrivate
{
    Q_OBJECT
public:
    QLowEnergyControllerPrivateBluezDBus(const QString& adapterPathWithPeripheralSupport = {});
    ~QLowEnergyControllerPrivateBluezDBus() override;

    void init() override;
    void connectToDevice() override;
    void disconnectFromDevice() override;

    void discoverServices() override;
    void discoverServiceDetails(const QBluetoothUuid &service,
                                QLowEnergyService::DiscoveryMode mode) override;

    void readCharacteristic(
                const QSharedPointer<QLowEnergyServicePrivate> service,
                const QLowEnergyHandle charHandle) override;
    void readDescriptor(
                const QSharedPointer<QLowEnergyServicePrivate> service,
                const QLowEnergyHandle charHandle,
                const QLowEnergyHandle descriptorHandle) override;

    void writeCharacteristic(
                const QSharedPointer<QLowEnergyServicePrivate> service,
                const QLowEnergyHandle charHandle,
                const QByteArray &newValue,
                QLowEnergyService::WriteMode writeMode) override;
    void writeDescriptor(
                const QSharedPointer<QLowEnergyServicePrivate> service,
                const QLowEnergyHandle charHandle,
                const QLowEnergyHandle descriptorHandle,
                const QByteArray &newValue) override;

    void startAdvertising(
                const QLowEnergyAdvertisingParameters &params,
                const QLowEnergyAdvertisingData &advertisingData,
                const QLowEnergyAdvertisingData &scanResponseData) override;
    void stopAdvertising() override;

    void requestConnectionUpdate(
                const QLowEnergyConnectionParameters & params) override;
    void addToGenericAttributeList(
                        const QLowEnergyServiceData &service,
                        QLowEnergyHandle startHandle) override;

    int mtu() const override;

private:
    void connectToDeviceHelper();
    void resetController();

    void scheduleNextJob();

private slots:
    void devicePropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
                                 const QStringList &invalidatedProperties);
    void characteristicPropertiesChanged(QLowEnergyHandle charHandle, const QString &interface,
                                    const QVariantMap &changedProperties,
                                    const QStringList &invalidatedProperties);
    void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces);

    void onCharReadFinished(QDBusPendingCallWatcher *call);
    void onDescReadFinished(QDBusPendingCallWatcher *call);
    void onCharWriteFinished(QDBusPendingCallWatcher *call);
    void onDescWriteFinished(QDBusPendingCallWatcher *call);
private:

    OrgBluezAdapter1Interface* adapter{};
    OrgBluezDevice1Interface* device{};
    OrgFreedesktopDBusObjectManagerInterface* managerBluez{};
    OrgFreedesktopDBusPropertiesInterface* deviceMonitor{};
    QString adapterPathWithPeripheralSupport;

    int remoteMtu{-1};
    QtBluezPeripheralApplication* peripheralApplication{};
    QtBluezPeripheralConnectionManager* peripheralConnectionManager{};
    QLeDBusAdvertiser *advertiser{};
    void handleAdvertisingError();
    void handlePeripheralApplicationError();
    void handlePeripheralApplicationRegistered();
    void handlePeripheralConnectivityChanged(bool connected);
    void handlePeripheralCharacteristicValueUpdate(QLowEnergyHandle handle,
                                                   const QByteArray& value);
    void handlePeripheralDescriptorValueUpdate(QLowEnergyHandle characteristicHandle,
                                               QLowEnergyHandle descriptorHandle,
                                               const QByteArray& value);
    void handlePeripheralRemoteDeviceChanged(const QBluetoothAddress& address,
                                             const QString& name,
                                             quint16 mtu);
    bool pendingConnect = false;
    bool disconnectSignalRequired = false;

    struct GattCharacteristic
    {
        QSharedPointer<OrgBluezGattCharacteristic1Interface> characteristic;
        QSharedPointer<OrgFreedesktopDBusPropertiesInterface> charMonitor;
        QList<QSharedPointer<OrgBluezGattDescriptor1Interface>> descriptors;
    };

    struct GattService
    {
        QString servicePath;
        QList<GattCharacteristic> characteristics;

        bool hasBatteryService = false;
        QSharedPointer<OrgBluezBattery1Interface> batteryInterface;
    };

    QHash<QBluetoothUuid, GattService> dbusServices;
    QLowEnergyHandle runningHandle = 1;

    struct GattJob {
        enum JobFlag {
            Unset                   = 0x00,
            CharRead                = 0x01,
            CharWrite               = 0x02,
            DescRead                = 0x04,
            DescWrite               = 0x08,
            ServiceDiscovery        = 0x10,
            LastServiceDiscovery    = 0x20
        };
        Q_DECLARE_FLAGS(JobFlags, JobFlag)

        JobFlags flags = GattJob::Unset;
        QLowEnergyHandle handle;
        QByteArray value;
        QLowEnergyService::WriteMode writeMode = QLowEnergyService::WriteWithResponse;
        QSharedPointer<QLowEnergyServicePrivate> service;
    };

    QList<GattJob> jobs;
    bool jobPending = false;

    void prepareNextJob();
    void discoverBatteryServiceDetails(GattService &dbusData,
                                       QSharedPointer<QLowEnergyServicePrivate> serviceData);
    void executeClose(QLowEnergyController::Error newError);
};

QT_END_NAMESPACE

#endif // QLOWENERGYCONTROLLERPRIVATEDBUS_P_H