summaryrefslogtreecommitdiffstats
path: root/tests/bttestui/btlocaldevice.h
blob: 4c093bdb2d1ac778b7ad2ed245cf8060de9c215b (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef BTLOCALDEVICE_H
#define BTLOCALDEVICE_H

#include <QtCore/QObject>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothLocalDevice>
#include <QtBluetooth/QBluetoothServer>
#include <QtBluetooth/QBluetoothServiceDiscoveryAgent>
#include <QtBluetooth/QBluetoothSocket>
#include <QtBluetooth/QLowEnergyController>
#include <QtBluetooth/QLowEnergyServiceData>

class BtLocalDevice : public QObject
{
    Q_OBJECT
public:
    explicit BtLocalDevice(QObject *parent = 0);
    ~BtLocalDevice();
    Q_PROPERTY(QString hostMode READ hostMode NOTIFY hostModeStateChanged)
    Q_PROPERTY(int secFlags READ secFlags WRITE setSecFlags NOTIFY secFlagsChanged)

    Q_PROPERTY(bool centralExists READ centralExists NOTIFY leChanged);
    Q_PROPERTY(bool centralSubscribed READ centralSubscribed NOTIFY leChanged);
    Q_PROPERTY(QByteArray centralState READ centralState NOTIFY leChanged);
    Q_PROPERTY(QByteArray centralError READ centralError NOTIFY leChanged);
    Q_PROPERTY(QByteArray centralServiceState READ centralServiceState NOTIFY leChanged);
    Q_PROPERTY(QByteArray centralServiceError READ centralServiceError NOTIFY leChanged);
    Q_PROPERTY(QByteArray centralRSSI READ centralRSSI NOTIFY leChanged);

    Q_PROPERTY(QByteArray peripheralState READ peripheralState NOTIFY leChanged);
    Q_PROPERTY(QByteArray peripheralError READ peripheralError NOTIFY leChanged);
    Q_PROPERTY(QByteArray peripheralServiceState READ peripheralServiceState NOTIFY leChanged);
    Q_PROPERTY(QByteArray peripheralServiceError READ peripheralServiceError NOTIFY leChanged);
    Q_PROPERTY(bool peripheralExists READ peripheralExists NOTIFY leChanged);

    QBluetooth::SecurityFlags secFlags() const;
    void setSecFlags(int);
    QString hostMode() const;

signals:
    void errorOccurred(QBluetoothLocalDevice::Error error);
    void hostModeStateChanged();
    void socketStateUpdate(int foobar);
    void secFlagsChanged();
    bool leChanged(); // Same signal used for LE changes for simplicity

public slots:
    //QBluetoothLocalDevice
    void dumpInformation();
    void dumpErrors();
    void powerOn();
    void reset();
    void setHostMode(int newMode);
    void requestPairingUpdate(bool isPairing);
    void pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing);
    void connected(const QBluetoothAddress &addr);
    void disconnected(const QBluetoothAddress &addr);
    void cycleSecurityFlags();

    //QBluetoothDeviceDiscoveryAgent
    void deviceDiscovered(const QBluetoothDeviceInfo &info);
    void deviceUpdated(const QBluetoothDeviceInfo &info,
                       QBluetoothDeviceInfo::Fields updateFields);
    void discoveryFinished();
    void discoveryCanceled();
    void discoveryError(QBluetoothDeviceDiscoveryAgent::Error error);
    void startDiscovery();
    void stopDiscovery();

    //QBluetoothServiceDiscoveryAgent
    void startServiceDiscovery(bool isMinimalDiscovery);
    void startTargettedServiceDiscovery();
    void stopServiceDiscovery();
    void serviceDiscovered(const QBluetoothServiceInfo &info);
    void serviceDiscoveryFinished();
    void serviceDiscoveryCanceled();
    void serviceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error error);
    void dumpServiceDiscovery();

    //QBluetoothSocket
    void connectToService();
    void connectToServiceViaSearch();
    void disconnectToService();
    void closeSocket();
    void abortSocket();
    void socketConnected();
    void socketDisconnected();
    void socketError(QBluetoothSocket::SocketError error);
    void socketStateChanged(QBluetoothSocket::SocketState);
    void dumpSocketInformation();
    void writeData();
    void readData();

    //QBluetoothServer
    void serverError(QBluetoothServer::Error error);
    void serverListenPort();
    void serverListenUuid();
    void serverClose();
    void serverNewConnection();
    void clientSocketDisconnected();
    void clientSocketReadyRead();
    void dumpServerInformation();

    //QLowEnergyController central
    void centralCreate();
    void centralConnect();
    void centralStartServiceDiscovery();
    void centralDiscoverServiceDetails();
    void centralWrite();
    void centralRead();
    void centralSubscribeUnsubscribe();
    void centralDelete();
    bool centralExists() const;
    bool centralSubscribed() const;
    QByteArray centralState() const;
    QByteArray centralServiceState() const;
    QByteArray centralError() const;
    QByteArray centralServiceError() const;
    void centralReadRSSI() const;
    QByteArray centralRSSI() const;

    //QLowEnergyController peripheral
    void peripheralCreate();
    void peripheralStartAdvertising();
    void peripheralStopAdvertising();
    void peripheralWrite();
    void peripheralRead();
    void peripheralDelete();
    bool peripheralExists() const;
    QByteArray peripheralState() const;
    QByteArray peripheralServiceState() const;
    QByteArray peripheralError() const;
    QByteArray peripheralServiceError() const;

    // QLowEnergyController misc
    void startLeDeviceDiscovery();
    void dumpLeInfo();

private:
    void dumpLocalDevice(QBluetoothLocalDevice *dev);

    QBluetoothLocalDevice *localDevice = nullptr;
    QBluetoothDeviceDiscoveryAgent *deviceAgent = nullptr;
    QBluetoothServiceDiscoveryAgent *serviceAgent = nullptr;
    QBluetoothSocket *socket = nullptr;
    QBluetoothServer *server = nullptr;
    QList<QBluetoothSocket *> serverSockets;
    QBluetoothServiceInfo serviceInfo;
    QList<QBluetoothServiceInfo> foundTestServers;
    QBluetooth::SecurityFlags securityFlags;

    std::unique_ptr<QLowEnergyController> leCentralController;
    std::unique_ptr<QLowEnergyController> lePeripheralController;
    std::unique_ptr<QLowEnergyService> lePeripheralService;
    std::unique_ptr<QLowEnergyService> leCentralService;
    QLowEnergyAdvertisingData leAdvertisingData;
    QLowEnergyServiceData leServiceData;
    QBluetoothDeviceInfo leRemotePeripheralDevice;
    QByteArray latestRSSI = "N/A";
};

#endif // BTLOCALDEVICE_H