1 | /* |
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
7 | */ |
8 | |
9 | #include "agentadaptor.h" |
10 | #include "adapter.h" |
11 | #include "agent.h" |
12 | #include "device.h" |
13 | #include "manager.h" |
14 | #include "request.h" |
15 | |
16 | #include <QDBusMessage> |
17 | #include <QDBusObjectPath> |
18 | |
19 | namespace BluezQt |
20 | { |
21 | AgentAdaptor::AgentAdaptor(Agent *parent, Manager *manager) |
22 | : QDBusAbstractAdaptor(parent) |
23 | , m_agent(parent) |
24 | , m_manager(manager) |
25 | { |
26 | } |
27 | |
28 | QString AgentAdaptor::RequestPinCode(const QDBusObjectPath &device, const QDBusMessage &msg) |
29 | { |
30 | msg.setDelayedReply(true); |
31 | Request<QString> req(OrgBluezAgent, msg); |
32 | |
33 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
34 | if (!dev) { |
35 | req.cancel(); |
36 | return QString(); |
37 | } |
38 | |
39 | m_agent->requestPinCode(device: dev, request: req); |
40 | return QString(); |
41 | } |
42 | |
43 | void AgentAdaptor::DisplayPinCode(const QDBusObjectPath &device, const QString &pincode) |
44 | { |
45 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
46 | if (!dev) { |
47 | return; |
48 | } |
49 | |
50 | m_agent->displayPinCode(device: dev, pinCode: pincode); |
51 | } |
52 | |
53 | quint32 AgentAdaptor::RequestPasskey(const QDBusObjectPath &device, const QDBusMessage &msg) |
54 | { |
55 | msg.setDelayedReply(true); |
56 | Request<quint32> req(OrgBluezAgent, msg); |
57 | |
58 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
59 | if (!dev) { |
60 | req.cancel(); |
61 | return 0; |
62 | } |
63 | |
64 | m_agent->requestPasskey(device: dev, request: req); |
65 | return 0; |
66 | } |
67 | |
68 | void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &device, quint32 passkey, quint16 entered) |
69 | { |
70 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
71 | if (!dev) { |
72 | return; |
73 | } |
74 | |
75 | m_agent->displayPasskey(device: dev, passkey: passkeyToString(passkey), entered: QString::number(entered)); |
76 | } |
77 | |
78 | void AgentAdaptor::RequestConfirmation(const QDBusObjectPath &device, quint32 passkey, const QDBusMessage &msg) |
79 | { |
80 | msg.setDelayedReply(true); |
81 | Request<> req(OrgBluezAgent, msg); |
82 | |
83 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
84 | if (!dev) { |
85 | req.cancel(); |
86 | return; |
87 | } |
88 | |
89 | m_agent->requestConfirmation(device: dev, passkey: passkeyToString(passkey), request: req); |
90 | } |
91 | |
92 | void AgentAdaptor::RequestAuthorization(const QDBusObjectPath &device, const QDBusMessage &msg) |
93 | { |
94 | msg.setDelayedReply(true); |
95 | Request<> req(OrgBluezAgent, msg); |
96 | |
97 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
98 | if (!dev) { |
99 | req.cancel(); |
100 | return; |
101 | } |
102 | |
103 | m_agent->requestAuthorization(device: dev, request: req); |
104 | } |
105 | |
106 | void AgentAdaptor::AuthorizeService(const QDBusObjectPath &device, const QString &uuid, const QDBusMessage &msg) |
107 | { |
108 | msg.setDelayedReply(true); |
109 | Request<> req(OrgBluezAgent, msg); |
110 | |
111 | DevicePtr dev = m_manager->deviceForUbi(ubi: device.path()); |
112 | if (!dev) { |
113 | req.cancel(); |
114 | return; |
115 | } |
116 | |
117 | m_agent->authorizeService(device: dev, uuid: uuid.toUpper(), request: req); |
118 | } |
119 | |
120 | void AgentAdaptor::Cancel() |
121 | { |
122 | m_agent->cancel(); |
123 | } |
124 | |
125 | void AgentAdaptor::Release() |
126 | { |
127 | m_agent->release(); |
128 | } |
129 | |
130 | QString AgentAdaptor::passkeyToString(quint32 passkey) const |
131 | { |
132 | // Passkey will always be a 6-digit number, zero-pad it at the start |
133 | return QStringLiteral("%1" ).arg(a: passkey, fieldWidth: 6, base: 10, fillChar: QLatin1Char('0')); |
134 | } |
135 | |
136 | } // namespace BluezQt |
137 | |
138 | #include "moc_agentadaptor.cpp" |
139 | |