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
|
/*
* Copyright (C) 2013-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#ifndef USS_BLUETOOTH_DEVICE_H
#define USS_BLUETOOTH_DEVICE_H
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusPendingCallWatcher>
#include <QSharedPointer>
#include <QString>
#include "freedesktop_properties.h"
#include "bluez_device1.h"
struct Device: QObject
{
Q_OBJECT
Q_PROPERTY(QString path
READ getPath
NOTIFY pathChanged)
Q_PROPERTY(QString name
READ getName
NOTIFY nameChanged)
Q_PROPERTY(QString iconName
READ getIconName
NOTIFY iconNameChanged)
Q_PROPERTY(QString address
READ getAddress
NOTIFY addressChanged)
Q_PROPERTY(Type type
READ getType
NOTIFY typeChanged)
Q_PROPERTY(bool paired
READ isPaired
NOTIFY pairedChanged)
Q_PROPERTY(bool trusted
READ isTrusted
WRITE makeTrusted
NOTIFY trustedChanged)
Q_PROPERTY(Connection connection
READ getConnection
NOTIFY connectionChanged)
Q_PROPERTY(Strength strength
READ getStrength
NOTIFY strengthChanged)
public:
enum Type { Other, Computer, Cellular, Smartphone, Phone, Modem, Network,
Headset, Speakers, Headphones, Video, OtherAudio, Joypad,
Keypad, Keyboard, Tablet, Mouse, Printer, Camera, Carkit, Watch };
enum Strength { None, Poor, Fair, Good, Excellent };
enum Connection { Disconnected=1, Connecting=2,
Connected=4, Disconnecting=8 };
Q_ENUMS(Type Strength Connection)
Q_DECLARE_FLAGS(Connections, Connection)
Q_SIGNALS:
void pathChanged();
void nameChanged();
void iconNameChanged();
void addressChanged();
void typeChanged();
void pairedChanged();
void trustedChanged();
void connectionChanged();
void strengthChanged();
void deviceChanged(); // catchall for any change
void pairingDone(bool success);
public:
const QString& getName() const { return m_name; }
const QString& getAddress() const { return m_address; }
const QString& getIconName() const { return m_iconName; }
Type getType() const { return m_type; }
bool isPaired() const { return m_paired; }
bool isTrusted() const { return m_trusted; }
Connection getConnection() const { return m_connection; }
Strength getStrength() const { return m_strength; }
QString getPath() const { return m_bluezDevice ? m_bluezDevice->path() : QString(); }
private:
QString m_name;
QString m_state;
QString m_address;
QString m_iconName;
QString m_fallbackIconName;
Type m_type = Type::Other;
bool m_paired = false;
bool m_trusted = false;
Connection m_connection = Connection::Disconnected;
Strength m_strength = Strength::None;
bool m_isConnected = false;
bool m_connectAfterPairing = false;
QScopedPointer<BluezDevice1> m_bluezDevice;
QScopedPointer<FreeDesktopProperties> m_bluezDeviceProperties;
bool m_isPairing = false;
protected:
void setName(const QString &name);
void setIconName(const QString &name);
void setAddress(const QString &address);
void setType(Type type);
void setPaired(bool paired);
void setTrusted(bool trusted);
void setConnection(Connection connection);
void setStrength(Strength strength);
void updateIcon();
void updateConnection();
public:
Device() {}
Device(const QString &path, QDBusConnection &bus);
~Device() {}
bool isValid() const { return getType() != Type::Other; }
void pair();
Q_INVOKABLE void cancelPairing();
void connect();
void makeTrusted(bool trusted);
void disconnect();
void setProperties(const QMap<QString,QVariant> &properties);
void setConnectAfterPairing(bool value);
private Q_SLOTS:
void slotPropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
const QStringList &invalidatedProperties);
void slotMakeTrustedDone(QDBusPendingCallWatcher *call);
private:
void initDevice(const QString &path, QDBusConnection &bus);
void updateProperties(QSharedPointer<QDBusInterface>);
void updateProperty(const QString &key, const QVariant &value);
static Type getTypeFromClass(quint32 bluetoothClass);
Device::Strength getStrengthFromRssi(int rssi);
void connectAfterPairing();
};
Q_DECLARE_METATYPE(Device*)
Q_DECLARE_OPERATORS_FOR_FLAGS(Device::Connections)
#endif // USS_BLUETOOTH_DEVICE_H
|