/
mcedevicelock.cpp
355 lines (295 loc) · 10.2 KB
/
mcedevicelock.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (C) 2016 Jolla Ltd
* Contact: Jonni Rainisto <jonni.rainisto@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "mcedevicelock.h"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QDBusPendingReply>
#include <QDebug>
#include <QFileInfo>
#include <sys/time.h>
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
namespace NemoDeviceLock
{
/** Maximum extra delay when waking up from suspend to apply devicelock */
#define DEVICELOCK_MAX_WAKEUP_DELAY_S 12
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#define qCInfo qCDebug
#endif
MceDeviceLock::MceDeviceLock(Authenticator::Methods allowedMethods, QObject *parent)
: HostDeviceLock(allowedMethods, parent)
, m_adaptor(this)
, m_mceRequest(
this,
systemBus(),
QStringLiteral(MCE_SERVICE),
QStringLiteral(MCE_REQUEST_PATH),
QStringLiteral(MCE_REQUEST_IF))
, m_locked(false)
, m_callActive(false)
, m_displayOn(true)
, m_tklockActive(true)
, m_userActivity(true)
{
connect(&m_hbTimer, &BackgroundActivity::running, this, &MceDeviceLock::lock);
trackMceProperty(
QStringLiteral(MCE_CALL_STATE_SIG),
SLOT(handleCallStateChanged(QString)),
QStringLiteral(MCE_CALL_STATE_GET),
&MceDeviceLock::handleCallStateChanged);
trackMceProperty(
QStringLiteral(MCE_DISPLAY_SIG),
SLOT(handleDisplayStateChanged(QString)),
QStringLiteral(MCE_DISPLAY_STATUS_GET),
&MceDeviceLock::handleDisplayStateChanged);
trackMceProperty(
QStringLiteral(MCE_TKLOCK_MODE_SIG),
SLOT(handleTklockStateChanged(QString)),
QStringLiteral(MCE_TKLOCK_MODE_GET),
&MceDeviceLock::handleTklockStateChanged);
systemBus().connectToSignal(
QString(),
QStringLiteral(MCE_SIGNAL_PATH),
QStringLiteral(MCE_SIGNAL_IF),
QStringLiteral(MCE_INACTIVITY_SIG),
this,
SLOT(handleInactivityStateChanged(bool)));
const auto response = m_mceRequest.call(QStringLiteral(MCE_INACTIVITY_STATUS_GET));
response->onFinished<bool>([this](bool state) {
handleInactivityStateChanged(state);
});
systemBus().registerObject(QStringLiteral("/devicelock"), this);
}
MceDeviceLock::~MceDeviceLock()
{
}
void MceDeviceLock::trackMceProperty(
const QString &changedSignal,
const char *changedSlot,
const QString &getMethod,
void (MceDeviceLock::*replySlot)(const QString &))
{
systemBus().connectToSignal(
QString(),
QStringLiteral(MCE_SIGNAL_PATH),
QStringLiteral(MCE_SIGNAL_IF),
changedSignal,
this,
changedSlot);
const auto response = m_mceRequest.call(getMethod);
response->onFinished<QString>([this, replySlot](const QString &state) {
(this->*replySlot)(state);
});
}
/** Handle tklock state signal/reply from mce
*/
void MceDeviceLock::handleTklockStateChanged(const QString &state)
{
bool active = (state == MCE_TK_LOCKED);
if (m_tklockActive != active) {
qCDebug(daemon, "MCE tklock state is now %s", qPrintable(state));
m_tklockActive = active;
setStateAndSetupLockTimer();
}
}
/** Handle call state signal/reply from mce
*/
void MceDeviceLock::handleCallStateChanged(const QString &state)
{
bool active = (state == MCE_CALL_STATE_ACTIVE || state == MCE_CALL_STATE_RINGING);
if (m_callActive != active) {
qCDebug(daemon, "MCE call state is now %s", qPrintable(state));
m_callActive = active;
setStateAndSetupLockTimer();
}
}
/** Handle display state signal/reply from mce
*/
void MceDeviceLock::handleDisplayStateChanged(const QString &state)
{
bool displayOn = (state == MCE_DISPLAY_ON_STRING || state == MCE_DISPLAY_DIM_STRING);
if (m_displayOn != displayOn) {
qCDebug(daemon, "MCE display state is now %s", qPrintable(state));
m_displayOn = displayOn;
setStateAndSetupLockTimer();
}
}
/** Handle inactivity state signal/reply from mce
*/
void MceDeviceLock::handleInactivityStateChanged(const bool state)
{
bool activity = !state;
if (m_userActivity != activity) {
qCDebug(daemon, "MCE inactivity state is now %s", activity ? "true" : "false");
m_userActivity = activity;
setStateAndSetupLockTimer();
}
}
/** Helper for producing human readable devicelock state logging
*/
static const char *reprLockState(bool locked)
{
return locked ? "Locked" : "Unlocked";
}
/** Evaluate initial devicelock state
*/
void MceDeviceLock::init()
{
setLocked(automaticLocking() >= 0);
stateChanged();
}
/** Evaluate devicelock state we should be in
*/
bool MceDeviceLock::getRequiredLockState()
{
/* Assume current state is ok */
bool locked = m_locked;
if (automaticLocking() < 0) {
/* Device locking is disabled */
locked = false;
} else if (automaticLocking() == 0 && !m_displayOn && !m_callActive) {
/* Display is off in immediate lock mode */
locked = true;
}
return locked;
}
/** Check if devicelock timer should be running
*/
bool MceDeviceLock::needLockTimer()
{
/* Must be currently unlocked */
if (m_locked)
return false;
/* Must not be disabled or in lock-immediate mode */
if (automaticLocking() <= 0)
return false;
/* Must not have active call */
if (m_callActive)
return false;
/* Must not be in active use */
if (m_displayOn && !m_tklockActive && m_userActivity)
return false;
return true;
}
/** Evaluate required devicelock state and/or need for timer
*/
void MceDeviceLock::setStateAndSetupLockTimer()
{
const bool requiredState = getRequiredLockState();
if (m_locked != requiredState) {
/* We should be in different deviceLockState. Set the state
* and assume that setState() recurses back here so that we
* get another chance to deal with the stable state. */
qCDebug(daemon, "forcing %s instead of %s",
reprLockState(requiredState), reprLockState(m_locked));
setLocked(requiredState);
} else if (needLockTimer()) {
/* Start devicelock timer */
if (!m_hbTimer.isWaiting()) {
int range_lo = automaticLocking() * 60;
int range_hi = range_lo + DEVICELOCK_MAX_WAKEUP_DELAY_S;
qCInfo(daemon, "start devicelock timer (%d-%d s)", range_lo, range_hi);
m_hbTimer.wait(range_lo, range_hi);
} else {
qCDebug(daemon, "devicelock timer already running");
}
} else {
/* Stop devicelock timer */
if (!m_hbTimer.isStopped()) {
qCInfo(daemon, "stop devicelock timer");
m_hbTimer.stop();
}
}
}
/** Slot for locking device on timer trigger
*/
void MceDeviceLock::lock()
{
qCInfo(daemon, "devicelock triggered");
setLocked(true);
/* The setState() call should end up terminating/restarting the
* timer. If that does not happen, it is a bug. Nevertheless, we
* must not leave an active cpu keepalive session behind. */
if (m_hbTimer.isRunning()) {
qCWarning(daemon, "cpu keepalive was not terminated; forcing stop");
m_hbTimer.stop();
}
}
bool MceDeviceLock::isLocked() const
{
return m_locked;
}
/** Explicitly set devicelock state
*/
void MceDeviceLock::setLocked(bool locked)
{
if (m_locked == locked) {
return;
}
qCInfo(daemon, "%s -> %s", reprLockState(m_locked), reprLockState(locked));
m_locked = locked;
stateChanged();
}
void MceDeviceLock::stateChanged()
{
setStateAndSetupLockTimer();
emit HostDeviceLock::stateChanged();
emit m_adaptor.stateChanged(state());
}
void MceDeviceLock::automaticLockingChanged()
{
setStateAndSetupLockTimer();
}
MceDeviceLockAdaptor::MceDeviceLockAdaptor(MceDeviceLock *deviceLock)
: QDBusAbstractAdaptor(deviceLock)
, m_deviceLock(deviceLock)
{
setAutoRelaySignals(true);
}
int MceDeviceLockAdaptor::state()
{
return m_deviceLock->state();
}
void MceDeviceLockAdaptor::setState(int state)
{
if (state != DeviceLock::Locked) {
// Unauthenticated unlocking is not accepted.
m_deviceLock->sendErrorReply(QDBusError::AccessDenied);
} else if (m_deviceLock->automaticLocking() == -1) {
m_deviceLock->sendErrorReply(QDBusError::AccessDenied, QStringLiteral("Device lock not in use"));
} else {
m_deviceLock->setLocked(true);
}
}
}