/
usb_moded-devicelock.c
471 lines (369 loc) · 13.9 KB
1
/**
2
3
4
* @file: usb_moded-devicelock.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
* Copyright (C) 2013-2019 Jolla Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Vesa Halttunen <vesa.halttunen@jollamobile.com>
* @author: Jonni Rainisto <jonni.rainisto@jollamobile.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
30
31
/*
32
33
* Interacts with the devicelock to know if we can expose the system contents or not
*/
34
35
#include "usb_moded-devicelock.h"
36
37
#include "usb_moded-control.h"
38
39
40
41
#include "usb_moded-dbus-private.h"
#include "usb_moded-log.h"
#include <string.h>
42
43
/* ========================================================================= *
44
* Types
45
46
47
* ========================================================================= */
/** Devicelock states */
48
typedef enum devicelock_state_t
49
50
{
/** Devicelock is not active */
51
DEVICELOCK_UNLOCKED = 0,
52
53
/** Devicelock is active */
54
DEVICELOCK_LOCKED = 1,
55
56
/** Initial startup value; from usb-moded p.o.v. equals locked */
57
DEVICELOCK_UNDEFINED = 2,
58
59
} devicelock_state_t;
60
61
62
63
/* ========================================================================= *
* Prototypes
* ========================================================================= */
64
65
66
/* ------------------------------------------------------------------------- *
* DEVICELOCK
* ------------------------------------------------------------------------- */
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
static const char *devicelock_state_repr (devicelock_state_t state);
bool devicelock_have_export_permission(void);
static void devicelock_state_changed (devicelock_state_t state);
static void devicelock_state_cancel (void);
static void devicelock_state_query_cb (DBusPendingCall *pending, void *aptr);
static void devicelock_state_query (void);
static void devicelock_state_signal (DBusMessage *msg);
static void devicelock_available_changed (const char *owner);
static void devicelock_available_cb (const char *owner);
static void devicelock_available_cancel (void);
static void devicelock_available_query (void);
static void devicelock_name_owner_signal (DBusMessage *msg);
static DBusHandlerResult devicelock_dbus_filter_cb (DBusConnection *con, DBusMessage *msg, void *aptr);
bool devicelock_start_listener (void);
void devicelock_stop_listener (void);
/* ========================================================================= *
* module state data
* ========================================================================= */
/* SystemBus connection ref used for devicelock ipc */
static DBusConnection *devicelock_con = NULL;
/* Cached devicelock state */
92
static devicelock_state_t devicelock_state = DEVICELOCK_UNDEFINED;
93
94
95
96
97
98
99
100
/* Flag for: devicelock is available on system bus */
static gboolean devicelock_is_available = FALSE;
/* ========================================================================= *
* devicelock state type
* ========================================================================= */
101
102
103
104
105
/** Return human readable representation of devicelock state enum
*/
static const char *
devicelock_state_repr(devicelock_state_t state)
{
106
107
LOG_REGISTER_CONTEXT;
108
const char *repr = "DEVICELOCK_<INVALID>";
109
110
111
switch( state )
{
112
113
114
case DEVICELOCK_UNLOCKED: repr = "DEVICELOCK_UNLOCKED"; break;
case DEVICELOCK_LOCKED: repr = "DEVICELOCK_LOCKED"; break;
case DEVICELOCK_UNDEFINED: repr = "DEVICELOCK_UNDEFINED"; break;
115
116
117
118
119
120
121
122
123
default: break;
}
return repr;
}
/* ========================================================================= *
* functionality provided to the other modules
* ========================================================================= */
124
125
/** Checks if the device is locked.
126
*
127
* @return TRUE for unlocked, FALSE for locked
128
129
*
*/
130
bool devicelock_have_export_permission(void)
131
{
132
133
LOG_REGISTER_CONTEXT;
134
bool unlocked = (devicelock_state == DEVICELOCK_UNLOCKED);
135
136
return unlocked;
137
138
}
139
140
141
142
143
/* ========================================================================= *
* devicelock state queries
* ========================================================================= */
static void devicelock_state_changed(devicelock_state_t state)
144
{
145
146
LOG_REGISTER_CONTEXT;
147
if( devicelock_state == state )
148
149
150
goto EXIT;
log_debug("devicelock state: %s -> %s",
151
devicelock_state_repr(devicelock_state),
152
devicelock_state_repr(state));
153
devicelock_state = state;
154
155
control_rethink_usb_charging_fallback();
156
157
158
EXIT:
return;
159
}
160
161
162
163
164
static DBusPendingCall *devicelock_state_query_pc = 0;
static void devicelock_state_cancel(void)
{
165
166
LOG_REGISTER_CONTEXT;
167
168
169
170
171
if( devicelock_state_query_pc ) {
dbus_pending_call_cancel(devicelock_state_query_pc);
dbus_pending_call_unref(devicelock_state_query_pc),
devicelock_state_query_pc = 0;
}
172
173
}
174
static void devicelock_state_query_cb(DBusPendingCall *pending, void *aptr)
175
{
176
177
LOG_REGISTER_CONTEXT;
178
DBusMessage *rsp = 0;
179
dbus_int32_t dta = DEVICELOCK_UNDEFINED;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
DBusError err = DBUS_ERROR_INIT;
(void)aptr;
if( !(rsp = dbus_pending_call_steal_reply(pending)) )
{
log_err("%s.%s: no reply",
DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ);
goto EXIT;
}
if( dbus_set_error_from_message(&err, rsp) )
{
log_err("%s.%s: error reply: %s: %s",
DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ,
err.name, err.message);
goto EXIT;
}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if( !dbus_message_get_args(rsp, &err, DBUS_TYPE_INT32, &dta,
DBUS_TYPE_INVALID) )
{
log_err("%s.%s: parse error: %s: %s",
DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ,
err.name, err.message);
goto EXIT;
}
EXIT:
devicelock_state_changed(dta);
if( rsp ) dbus_message_unref(rsp);
dbus_error_free(&err);
dbus_pending_call_unref(devicelock_state_query_pc),
devicelock_state_query_pc = 0;
217
218
}
219
220
static void devicelock_state_query(void)
{
221
222
LOG_REGISTER_CONTEXT;
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
DBusMessage *req = NULL;
DBusPendingCall *pc = 0;
devicelock_state_cancel();
log_debug("querying device lock state");
if( !devicelock_con ) {
log_err("not connected to system bus; skip device state query");
goto EXIT;
}
req = dbus_message_new_method_call(DEVICELOCK_SERVICE,
DEVICELOCK_OBJECT,
DEVICELOCK_INTERFACE,
DEVICELOCK_GET_STATE_REQ);
if( !req )
{
log_err("%s.%s: failed to construct request",
DEVICELOCK_INTERFACE, DEVICELOCK_GET_STATE_REQ);
goto EXIT;
}
if( !dbus_connection_send_with_reply(devicelock_con, req, &pc, -1) )
goto EXIT;
if( !pc )
goto EXIT;
if( !dbus_pending_call_set_notify(pc, devicelock_state_query_cb, 0, 0) )
goto EXIT;
devicelock_state_query_pc = pc, pc = 0;
EXIT:
if( pc ) dbus_pending_call_unref(pc);
if( req ) dbus_message_unref(req);
}
static void devicelock_state_signal(DBusMessage *msg)
{
266
267
LOG_REGISTER_CONTEXT;
268
DBusError err = DBUS_ERROR_INIT;
269
dbus_int32_t dta = DEVICELOCK_LOCKED;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_INT32, &dta,
DBUS_TYPE_INVALID) )
{
log_err("failed to parse %s.%s signal: %s: %s",
DEVICELOCK_INTERFACE, DEVICELOCK_STATE_CHANGED_SIG,
err.name, err.message);
}
devicelock_state_changed(dta);
dbus_error_free(&err);
}
/* ========================================================================= *
* devicelock name owner tracking
* ========================================================================= */
static void devicelock_available_changed(const char *owner)
{
291
292
LOG_REGISTER_CONTEXT;
293
294
295
296
297
298
299
300
gboolean is_available = (owner && *owner);
if( devicelock_is_available != is_available ) {
devicelock_is_available = is_available;
log_debug("devicelock is %s",
devicelock_is_available ? "running" : "stopped");
/* Forget cached device state */
301
devicelock_state_changed(DEVICELOCK_UNDEFINED);
302
303
304
305
306
307
308
309
310
311
312
313
/* Query current state on devicelock startup */
if( devicelock_is_available ) {
devicelock_state_query();
}
}
}
static DBusPendingCall *devicelock_available_pc = 0;
static void devicelock_available_cb(const char *owner)
{
314
315
LOG_REGISTER_CONTEXT;
316
317
318
319
320
321
322
323
devicelock_available_changed(owner);
dbus_pending_call_unref(devicelock_available_pc),
devicelock_available_pc = 0;
}
static void devicelock_available_cancel(void)
{
324
325
LOG_REGISTER_CONTEXT;
326
327
if( devicelock_available_pc )
{
328
dbus_pending_call_cancel(devicelock_available_pc);
329
330
331
332
333
334
335
dbus_pending_call_unref(devicelock_available_pc),
devicelock_available_pc = 0;
}
}
static void devicelock_available_query(void)
{
336
337
LOG_REGISTER_CONTEXT;
338
339
340
341
devicelock_available_cancel();
log_debug("querying %s name owner", DEVICELOCK_SERVICE);
342
343
344
umdbus_get_name_owner_async(DEVICELOCK_SERVICE,
devicelock_available_cb,
&devicelock_available_pc);
345
346
}
347
static void devicelock_name_owner_signal(DBusMessage *msg)
348
{
349
350
LOG_REGISTER_CONTEXT;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
DBusError err = DBUS_ERROR_INIT;
const char *name = 0;
const char *prev = 0;
const char *curr = 0;
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &prev,
DBUS_TYPE_STRING, &curr,
DBUS_TYPE_INVALID) )
{
log_err("failed to parse signal: %s: %s",
err.name, err.message);
}
else if( !strcmp(name, DEVICELOCK_SERVICE) )
{
devicelock_available_changed(curr);
}
dbus_error_free(&err);
}
/* ========================================================================= *
* dbus message filter
* ========================================================================= */
static DBusHandlerResult
devicelock_dbus_filter_cb(DBusConnection *con, DBusMessage *msg, void *aptr)
{
379
380
LOG_REGISTER_CONTEXT;
381
382
383
384
385
386
387
388
389
390
391
392
393
(void)con;
(void)aptr;
if( dbus_message_is_signal(msg,
DEVICELOCK_INTERFACE,
DEVICELOCK_STATE_CHANGED_SIG) )
{
devicelock_state_signal(msg);
}
else if( dbus_message_is_signal(msg,
DBUS_INTERFACE_DBUS,
DBUS_NAME_OWNER_CHANGED_SIG) )
{
394
devicelock_name_owner_signal(msg);
395
396
397
398
399
400
401
402
403
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/* ========================================================================= *
* start/stop devicelock state tracking
* ========================================================================= */
404
405
bool
devicelock_start_listener(void)
406
{
407
408
LOG_REGISTER_CONTEXT;
409
bool ack = false;
410
411
412
413
log_debug("starting devicelock listener");
/* Get connection ref */
414
if( (devicelock_con = umdbus_get_connection()) == 0 )
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
{
log_err("Could not connect to dbus for devicelock\n");
goto cleanup;
}
/* Add filter callback */
if( !dbus_connection_add_filter(devicelock_con,
devicelock_dbus_filter_cb , 0, 0) )
{
log_err("adding system dbus filter for devicelock failed");
goto cleanup;
}
/* Add match without blocking / error checking */
dbus_bus_add_match(devicelock_con, DEVICELOCK_STATE_CHANGED_MATCH,0);
dbus_bus_add_match(devicelock_con, DEVICELOCK_NAME_OWNER_CHANGED_MATCH,0);
/* Initiate async devicelock name owner query */
devicelock_available_query();
435
ack = true;
436
437
cleanup:
438
439
return ack;
440
441
}
442
void
443
devicelock_stop_listener(void)
444
{
445
446
LOG_REGISTER_CONTEXT;
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
log_debug("stopping devicelock listener");
/* Do note leave pending queries behind */
devicelock_state_cancel();
devicelock_available_cancel();
if(devicelock_con)
{
/* Remove filter callback */
dbus_connection_remove_filter(devicelock_con,
devicelock_dbus_filter_cb, 0);
if( dbus_connection_get_is_connected(devicelock_con) ) {
/* Remove match without blocking / error checking */
dbus_bus_remove_match(devicelock_con,
DEVICELOCK_STATE_CHANGED_MATCH, 0);
dbus_bus_remove_match(devicelock_con,
DEVICELOCK_NAME_OWNER_CHANGED_MATCH, 0);
}
/* Let go of connection ref */
dbus_connection_unref(devicelock_con),
devicelock_con = 0;
}
}