Changeset View
Changeset View
Standalone View
Standalone View
autotests/bolt/fakeserver/fakedevice.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "fakedevice.h" | ||||
22 | #include "fakedeviceadaptor.h" | ||||
23 | | ||||
24 | #include <QDBusConnection> | ||||
25 | #include <QDBusError> | ||||
26 | | ||||
27 | #include <chrono> | ||||
28 | | ||||
29 | using namespace std::chrono_literals; | ||||
30 | | ||||
31 | FakeDevice::FakeDevice(const QString &uid, QObject *parent) | ||||
32 | : QObject(parent) | ||||
33 | , mUid(uid) | ||||
34 | { | ||||
35 | new FakeDeviceAdaptor(this); | ||||
36 | auto bus = QDBusConnection::sessionBus(); | ||||
37 | if (!bus.registerObject(dbusPath().path(), this)) { | ||||
38 | throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2") | ||||
39 | .arg(mUid, bus.lastError().message())); | ||||
40 | } | ||||
41 | } | ||||
42 | | ||||
43 | FakeDevice::FakeDevice(const QJsonObject &json, QObject *parent) | ||||
44 | : QObject(parent) | ||||
45 | , mUid(json[QStringLiteral("Uid")].toString()) | ||||
46 | , mName(json[QStringLiteral("Name")].toString()) | ||||
47 | , mVendor(json[QStringLiteral("Vendor")].toString()) | ||||
48 | , mType(json[QStringLiteral("Type")].toString()) | ||||
49 | , mStatus(json[QStringLiteral("Status")].toString()) | ||||
50 | , mAuthFlags(json[QStringLiteral("AuthFlags")].toString()) | ||||
51 | , mParent(json[QStringLiteral("Parent")].toString()) | ||||
52 | , mSysfsPath(json[QStringLiteral("SysfsPath")].toString()) | ||||
53 | , mPolicy(json[QStringLiteral("Policy")].toString()) | ||||
54 | , mKey(json[QStringLiteral("Key")].toString()) | ||||
55 | , mLabel(json[QStringLiteral("Label")].toString()) | ||||
56 | , mConnectTime(json[QStringLiteral("ConnectTime")].toVariant().value<quint64>()) | ||||
57 | , mAuthorizeTime(json[QStringLiteral("AuthorizeTime")].toVariant().value<quint64>()) | ||||
58 | , mStoreTime(json[QStringLiteral("StoreTime")].toVariant().value<quint64>()) | ||||
59 | , mStored(json[QStringLiteral("Stored")].toBool()) | ||||
60 | { | ||||
61 | new FakeDeviceAdaptor(this); | ||||
62 | auto bus = QDBusConnection::sessionBus(); | ||||
63 | if (!bus.registerObject(dbusPath().path(), this)) { | ||||
64 | throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2") | ||||
65 | .arg(mUid, bus.lastError().message())); | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | FakeDevice::~FakeDevice() | ||||
70 | { | ||||
71 | QDBusConnection::sessionBus().unregisterObject(dbusPath().path()); | ||||
72 | } | ||||
73 | | ||||
74 | QDBusObjectPath FakeDevice::dbusPath() const | ||||
75 | { | ||||
76 | return QDBusObjectPath(QStringLiteral("/org/freedesktop/bolt/devices/%1") | ||||
77 | .arg(QString(mUid).replace(QLatin1Char('-'), QLatin1Char('_')))); | ||||
78 | } | ||||
79 | | ||||
80 | QString FakeDevice::uid() const | ||||
81 | { | ||||
82 | return mUid; | ||||
83 | } | ||||
84 | | ||||
85 | QString FakeDevice::name() const | ||||
86 | { | ||||
87 | return mName; | ||||
88 | } | ||||
89 | | ||||
90 | void FakeDevice::setName(const QString &name) | ||||
91 | { | ||||
92 | mName = name; | ||||
93 | } | ||||
94 | | ||||
95 | QString FakeDevice::vendor() const | ||||
96 | { | ||||
97 | return mVendor; | ||||
98 | } | ||||
99 | | ||||
100 | void FakeDevice::setVendor(const QString &vendor) | ||||
101 | { | ||||
102 | mVendor = vendor; | ||||
103 | } | ||||
104 | | ||||
105 | QString FakeDevice::type() const | ||||
106 | { | ||||
107 | return mType; | ||||
108 | } | ||||
109 | | ||||
110 | void FakeDevice::setType(const QString &type) | ||||
111 | { | ||||
112 | mType = type; | ||||
113 | } | ||||
114 | | ||||
115 | QString FakeDevice::status() const | ||||
116 | { | ||||
117 | return mStatus; | ||||
118 | } | ||||
119 | | ||||
120 | void FakeDevice::setStatus(const QString &status) | ||||
121 | { | ||||
122 | mStatus = status; | ||||
123 | } | ||||
124 | | ||||
125 | QString FakeDevice::authFlags() const | ||||
126 | { | ||||
127 | return mAuthFlags; | ||||
128 | } | ||||
129 | | ||||
130 | void FakeDevice::setAuthFlags(const QString &authFlags) | ||||
131 | { | ||||
132 | mAuthFlags = authFlags; | ||||
133 | } | ||||
134 | | ||||
135 | QString FakeDevice::parent() const | ||||
136 | { | ||||
137 | return mParent; | ||||
138 | } | ||||
139 | | ||||
140 | void FakeDevice::setParent(const QString &parent) | ||||
141 | { | ||||
142 | mParent = parent; | ||||
143 | } | ||||
144 | | ||||
145 | QString FakeDevice::sysfsPath() const | ||||
146 | { | ||||
147 | return mSysfsPath; | ||||
148 | } | ||||
149 | | ||||
150 | void FakeDevice::setSysfsPath(const QString &sysfsPath) | ||||
151 | { | ||||
152 | mSysfsPath = sysfsPath; | ||||
153 | } | ||||
154 | | ||||
155 | bool FakeDevice::stored() const | ||||
156 | { | ||||
157 | return mStored; | ||||
158 | } | ||||
159 | | ||||
160 | void FakeDevice::setStored(bool stored) | ||||
161 | { | ||||
162 | mStored = stored; | ||||
163 | } | ||||
164 | | ||||
165 | QString FakeDevice::policy() const | ||||
166 | { | ||||
167 | return mPolicy; | ||||
168 | } | ||||
169 | | ||||
170 | void FakeDevice::setPolicy(const QString &policy) | ||||
171 | { | ||||
172 | mPolicy = policy; | ||||
173 | } | ||||
174 | | ||||
175 | QString FakeDevice::key() const | ||||
176 | { | ||||
177 | return mKey; | ||||
178 | } | ||||
179 | | ||||
180 | void FakeDevice::setKey(const QString &key) | ||||
181 | { | ||||
182 | mKey = key; | ||||
183 | } | ||||
184 | | ||||
185 | QString FakeDevice::label() const | ||||
186 | { | ||||
187 | return mLabel; | ||||
188 | } | ||||
189 | | ||||
190 | void FakeDevice::setLabel(const QString &label) | ||||
191 | { | ||||
192 | mLabel = label; | ||||
193 | } | ||||
194 | | ||||
195 | quint64 FakeDevice::connectTime() const | ||||
196 | { | ||||
197 | return mConnectTime; | ||||
198 | } | ||||
199 | | ||||
200 | void FakeDevice::setConnectTime(quint64 connectTime) | ||||
201 | { | ||||
202 | mConnectTime = connectTime; | ||||
203 | } | ||||
204 | | ||||
205 | quint64 FakeDevice::authorizeTime() const | ||||
206 | { | ||||
207 | return mAuthorizeTime; | ||||
208 | } | ||||
209 | | ||||
210 | void FakeDevice::setAuthorizeTime(quint64 authorizeTime) | ||||
211 | { | ||||
212 | mAuthorizeTime = authorizeTime; | ||||
213 | } | ||||
214 | | ||||
215 | quint64 FakeDevice::storeTime() const | ||||
216 | { | ||||
217 | return mStoreTime; | ||||
218 | } | ||||
219 | | ||||
220 | void FakeDevice::setStoreTime(quint64 storeTime) | ||||
221 | { | ||||
222 | mStoreTime = storeTime; | ||||
223 | } | ||||
224 | | ||||
225 | void FakeDevice::Authorize(const QString &flags) | ||||
226 | { | ||||
227 | std::this_thread::sleep_for(1s); // simulate this operation taking time | ||||
228 | mAuthFlags = flags; | ||||
229 | mStatus = QLatin1Literal("authorized"); | ||||
230 | } |