forked from piggz/harbour-amazfish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqbleservice.cpp
163 lines (129 loc) · 4.66 KB
/
qbleservice.cpp
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
#include "qbleservice.h"
#include <QtXml/QtXml>
#include <utility>
QBLEService::QBLEService(const QString &uuid,const QString &path, QObject *parent) : QObject(parent), m_serviceUUID(uuid), m_servicePath(path)
{
m_serviceInterface = new QDBusInterface("org.bluez", m_servicePath, "org.bluez.GattService1", QDBusConnection::systemBus());
introspect();
}
void QBLEService::characteristicChangedInt(const QString &characteristic, const QByteArray &value)
{
//qDebug() << "characteristicChangedInt:" << characteristic << value.size() << value.toHex();
emit characteristicChanged(characteristic, value);
}
void QBLEService::characteristicReadInt(const QString &characteristic, const QByteArray &value)
{
//qDebug() << "characteristicReadInt:" << characteristic << value.size() << value.toHex();
emit characteristicRead(characteristic, value);
}
void QBLEService::characteristicWrittenInt(const QString &characteristic, const QByteArray &value)
{
//qDebug() << "characteristicWrittenInt:" << characteristic << value.size() << value.toHex();
emit characteristicRead(characteristic, value);
}
void QBLEService::descriptorWrittenInt(const QString &descriptor, const QByteArray &value)
{
//qDebug() << "descriptorWrittenInt:" << descriptor << value.size() << value.toHex();
emit descriptorWritten(descriptor, value);
}
void QBLEService::enableNotification(const QString &c)
{
qDebug() << "Starting notify for " << c;
QBLECharacteristic *ch = characteristic(c);
if (ch) {
ch->startNotify();
} else {
qDebug() << "Unable to get characteristic";
}
}
void QBLEService::disableNotification(const QString &c)
{
qDebug() << "Stopping notify for " << c;
QBLECharacteristic *ch = characteristic(c);
if (ch) {
ch->stopNotify();
} else {
qDebug() << "Unable to get characteristic";
}
}
QString QBLEService::serviceUUID() const
{
return m_serviceUUID;
}
void QBLEService::writeValue(const QString &c, const QByteArray &value)
{
qDebug() << "Writing to " << c << ":" << value.toHex();
QBLECharacteristic *ch = characteristic(c);
if (ch) {
ch->writeValue(value);
} else {
qDebug() << "Unable to get characteristic";
}
}
void QBLEService::writeAsync(const QString &c, const QByteArray &value)
{
qDebug() << "Async Writing to " << c << ":" << value.toHex();
QBLECharacteristic *ch = characteristic(c);
if (ch) {
ch->writeAsync(value);
} else {
qDebug() << "Unable to get characteristic";
}
}
QByteArray QBLEService::readValue(const QString &c)
{
qDebug() << "Reading from " << c;
QBLECharacteristic *ch = characteristic(c);
if (ch) {
return ch->readValue();
} else {
qDebug() << "Unable to get characteristic";
}
return QByteArray();
}
void QBLEService::introspect()
{
QDBusInterface miIntro("org.bluez", m_servicePath, "org.freedesktop.DBus.Introspectable", QDBusConnection::systemBus(), 0);
QDBusReply<QString> xml = miIntro.call("Introspect");
QDomDocument doc;
doc.setContent(xml.value());
QDomNodeList nodes = doc.elementsByTagName("node");
qDebug() << nodes.count() << "nodes";
for (int x = 0; x < nodes.count(); x++)
{
QDomElement node = nodes.at(x).toElement();
QString nodeName = node.attribute("name");
if (nodeName.startsWith("char")) {
QString path = m_servicePath + "/" + nodeName;
QDBusInterface charInterface("org.bluez", path, "org.bluez.GattCharacteristic1", QDBusConnection::systemBus(), 0);
m_characteristicMap[charInterface.property("UUID").toString()] = new QBLECharacteristic(path, this);
}
}
for (const auto &c: static_cast<const QMap<QString, QBLECharacteristic* >>(m_characteristicMap)) {
connect(c, &QBLECharacteristic::characteristicChanged, this, &QBLEService::characteristicChangedInt);
connect(c, &QBLECharacteristic::characteristicRead, this, &QBLEService::characteristicReadInt);
}
qDebug() << "Introspect:characteristics:" << m_characteristicMap.keys();
}
void QBLEService::onPropertiesChanged(const QString &interface, const QVariantMap &map, const QStringList &list)
{
emit propertiesChanged(interface, map, list);
}
QBLECharacteristic *QBLEService::characteristic(const QString &c) const
{
return m_characteristicMap.value(c, nullptr);
}
void QBLEService::readAsync(const QString &c) const
{
qDebug() << "Async reading from " << c;
QBLECharacteristic *ch = characteristic(c);
if (ch) {
ch->readAsync();
} else {
qDebug() << "Unable to get characteristic";
}
}
bool QBLEService::operationRunning()
{
return false;
}