1 | /* |
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
7 | */ |
8 | |
9 | #include "gattcharacteristic.h" |
10 | #include "gattcharacteristic_p.h" |
11 | #include "gattservice.h" |
12 | #include "utils.h" |
13 | |
14 | namespace BluezQt |
15 | { |
16 | GattCharacteristic::GattCharacteristic(const QString &uuid, GattService *service) |
17 | : GattCharacteristic(uuid, {QLatin1String("read" ), QLatin1String("write" )}, service) |
18 | { |
19 | } |
20 | |
21 | GattCharacteristic::GattCharacteristic(const QString &uuid, const QStringList &flags, GattService *service) |
22 | : QObject(service) |
23 | , d(new GattCharacterisiticPrivate(uuid, flags, service)) |
24 | { |
25 | } |
26 | |
27 | GattCharacteristic::~GattCharacteristic() = default; |
28 | |
29 | QByteArray GattCharacteristic::readValue() |
30 | { |
31 | if (d->m_readCallback) { |
32 | d->m_value = d->m_readCallback(); |
33 | } |
34 | |
35 | return d->m_value; |
36 | } |
37 | |
38 | void GattCharacteristic::writeValue(const QByteArray &value) |
39 | { |
40 | d->m_value = value; |
41 | |
42 | if (isNotifying()) { |
43 | d->emitPropertyChanged(updatedProperties: {{QLatin1String("Value" ), value}}); |
44 | } |
45 | |
46 | Q_EMIT valueWritten(value: d->m_value); |
47 | } |
48 | |
49 | QString GattCharacteristic::uuid() const |
50 | { |
51 | return d->m_uuid; |
52 | } |
53 | |
54 | const GattService *GattCharacteristic::service() const |
55 | { |
56 | return d->m_service; |
57 | } |
58 | |
59 | QStringList GattCharacteristic::flags() const |
60 | { |
61 | return d->m_flags; |
62 | } |
63 | |
64 | void GattCharacteristic::startNotify() |
65 | { |
66 | if (d->m_canNotify) { |
67 | d->m_notifying = true; |
68 | } |
69 | } |
70 | |
71 | void GattCharacteristic::stopNotify() |
72 | { |
73 | d->m_notifying = false; |
74 | } |
75 | |
76 | bool GattCharacteristic::isNotifying() const |
77 | { |
78 | return d->m_notifying; |
79 | } |
80 | |
81 | QDBusObjectPath GattCharacteristic::objectPath() const |
82 | { |
83 | return d->m_objectPath; |
84 | } |
85 | |
86 | void GattCharacteristic::setReadCallback(ReadCallback callback) |
87 | { |
88 | d->m_readCallback = callback; |
89 | } |
90 | |
91 | } // namespace BluezQt |
92 | |
93 | #include "moc_gattcharacteristic.cpp" |
94 | |