Skip to content

Latest commit

 

History

History
471 lines (369 loc) · 13.9 KB

usb_moded-devicelock.c

File metadata and controls

471 lines (369 loc) · 13.9 KB
 
Mar 22, 2011
Mar 22, 2011
1
/**
Aug 24, 2018
Aug 24, 2018
2
3
4
* @file: usb_moded-devicelock.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
Apr 9, 2019
Apr 9, 2019
5
* Copyright (C) 2013-2019 Jolla Ltd.
Aug 24, 2018
Aug 24, 2018
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
*/
Nov 7, 2016
Nov 7, 2016
30
Mar 22, 2011
Mar 22, 2011
31
/*
Nov 7, 2016
Nov 7, 2016
32
33
* Interacts with the devicelock to know if we can expose the system contents or not
*/
Mar 22, 2011
Mar 22, 2011
34
35
#include "usb_moded-devicelock.h"
Sep 5, 2018
Sep 5, 2018
36
Sep 5, 2018
Sep 5, 2018
37
#include "usb_moded-control.h"
Sep 5, 2018
Sep 5, 2018
38
39
40
41
#include "usb_moded-dbus-private.h"
#include "usb_moded-log.h"
#include <string.h>
Nov 7, 2016
Nov 7, 2016
42
43
/* ========================================================================= *
Aug 24, 2018
Aug 24, 2018
44
* Types
Nov 7, 2016
Nov 7, 2016
45
46
47
* ========================================================================= */
/** Devicelock states */
Sep 5, 2018
Sep 5, 2018
48
typedef enum devicelock_state_t
Nov 7, 2016
Nov 7, 2016
49
50
{
/** Devicelock is not active */
Sep 5, 2018
Sep 5, 2018
51
DEVICELOCK_UNLOCKED = 0,
Nov 7, 2016
Nov 7, 2016
52
53
/** Devicelock is active */
Sep 5, 2018
Sep 5, 2018
54
DEVICELOCK_LOCKED = 1,
Nov 7, 2016
Nov 7, 2016
55
56
/** Initial startup value; from usb-moded p.o.v. equals locked */
Sep 5, 2018
Sep 5, 2018
57
DEVICELOCK_UNDEFINED = 2,
Nov 7, 2016
Nov 7, 2016
58
59
} devicelock_state_t;
Aug 24, 2018
Aug 24, 2018
60
61
62
63
/* ========================================================================= *
* Prototypes
* ========================================================================= */
Apr 9, 2019
Apr 9, 2019
64
65
66
/* ------------------------------------------------------------------------- *
* DEVICELOCK
* ------------------------------------------------------------------------- */
Aug 24, 2018
Aug 24, 2018
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 */
Sep 5, 2018
Sep 5, 2018
92
static devicelock_state_t devicelock_state = DEVICELOCK_UNDEFINED;
Aug 24, 2018
Aug 24, 2018
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
* ========================================================================= */
Nov 7, 2016
Nov 7, 2016
101
102
103
104
105
/** Return human readable representation of devicelock state enum
*/
static const char *
devicelock_state_repr(devicelock_state_t state)
{
Feb 20, 2019
Feb 20, 2019
106
107
LOG_REGISTER_CONTEXT;
Sep 5, 2018
Sep 5, 2018
108
const char *repr = "DEVICELOCK_<INVALID>";
Mar 22, 2011
Mar 22, 2011
109
Nov 7, 2016
Nov 7, 2016
110
111
switch( state )
{
Sep 5, 2018
Sep 5, 2018
112
113
114
case DEVICELOCK_UNLOCKED: repr = "DEVICELOCK_UNLOCKED"; break;
case DEVICELOCK_LOCKED: repr = "DEVICELOCK_LOCKED"; break;
case DEVICELOCK_UNDEFINED: repr = "DEVICELOCK_UNDEFINED"; break;
Nov 7, 2016
Nov 7, 2016
115
116
117
118
119
120
121
122
123
default: break;
}
return repr;
}
/* ========================================================================= *
* functionality provided to the other modules
* ========================================================================= */
Oct 22, 2013
Oct 22, 2013
124
Mar 22, 2011
Mar 22, 2011
125
/** Checks if the device is locked.
Nov 7, 2016
Nov 7, 2016
126
*
Feb 14, 2017
Feb 14, 2017
127
* @return TRUE for unlocked, FALSE for locked
Mar 22, 2011
Mar 22, 2011
128
129
*
*/
Aug 24, 2018
Aug 24, 2018
130
bool devicelock_have_export_permission(void)
Mar 22, 2011
Mar 22, 2011
131
{
Feb 20, 2019
Feb 20, 2019
132
133
LOG_REGISTER_CONTEXT;
Sep 5, 2018
Sep 5, 2018
134
bool unlocked = (devicelock_state == DEVICELOCK_UNLOCKED);
Nov 7, 2016
Nov 7, 2016
135
Feb 14, 2017
Feb 14, 2017
136
return unlocked;
Mar 22, 2011
Mar 22, 2011
137
138
}
Nov 7, 2016
Nov 7, 2016
139
140
141
142
143
/* ========================================================================= *
* devicelock state queries
* ========================================================================= */
static void devicelock_state_changed(devicelock_state_t state)
Mar 22, 2011
Mar 22, 2011
144
{
Feb 20, 2019
Feb 20, 2019
145
146
LOG_REGISTER_CONTEXT;
Sep 5, 2018
Sep 5, 2018
147
if( devicelock_state == state )
Feb 14, 2017
Feb 14, 2017
148
149
150
goto EXIT;
log_debug("devicelock state: %s -> %s",
Sep 5, 2018
Sep 5, 2018
151
devicelock_state_repr(devicelock_state),
Feb 14, 2017
Feb 14, 2017
152
devicelock_state_repr(state));
Sep 5, 2018
Sep 5, 2018
153
devicelock_state = state;
Feb 14, 2017
Feb 14, 2017
154
Sep 5, 2018
Sep 5, 2018
155
control_rethink_usb_charging_fallback();
Feb 14, 2017
Feb 14, 2017
156
157
158
EXIT:
return;
Nov 7, 2016
Nov 7, 2016
159
}
Mar 22, 2011
Mar 22, 2011
160
Nov 7, 2016
Nov 7, 2016
161
162
163
164
static DBusPendingCall *devicelock_state_query_pc = 0;
static void devicelock_state_cancel(void)
{
Feb 20, 2019
Feb 20, 2019
165
166
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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;
}
Mar 22, 2011
Mar 22, 2011
172
173
}
Nov 7, 2016
Nov 7, 2016
174
static void devicelock_state_query_cb(DBusPendingCall *pending, void *aptr)
Oct 22, 2013
Oct 22, 2013
175
{
Feb 20, 2019
Feb 20, 2019
176
177
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
178
DBusMessage *rsp = 0;
Sep 5, 2018
Sep 5, 2018
179
dbus_int32_t dta = DEVICELOCK_UNDEFINED;
Nov 7, 2016
Nov 7, 2016
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;
}
Oct 22, 2013
Oct 22, 2013
198
Nov 7, 2016
Nov 7, 2016
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;
Oct 22, 2013
Oct 22, 2013
217
218
}
Nov 7, 2016
Nov 7, 2016
219
220
static void devicelock_state_query(void)
{
Feb 20, 2019
Feb 20, 2019
221
222
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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)
{
Feb 20, 2019
Feb 20, 2019
266
267
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
268
DBusError err = DBUS_ERROR_INIT;
Sep 5, 2018
Sep 5, 2018
269
dbus_int32_t dta = DEVICELOCK_LOCKED;
Nov 7, 2016
Nov 7, 2016
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)
{
Feb 20, 2019
Feb 20, 2019
291
292
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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 */
Sep 5, 2018
Sep 5, 2018
301
devicelock_state_changed(DEVICELOCK_UNDEFINED);
Nov 7, 2016
Nov 7, 2016
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)
{
Feb 20, 2019
Feb 20, 2019
314
315
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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)
{
Feb 20, 2019
Feb 20, 2019
324
325
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
326
327
if( devicelock_available_pc )
{
Aug 24, 2018
Aug 24, 2018
328
dbus_pending_call_cancel(devicelock_available_pc);
Nov 7, 2016
Nov 7, 2016
329
330
331
332
333
334
335
dbus_pending_call_unref(devicelock_available_pc),
devicelock_available_pc = 0;
}
}
static void devicelock_available_query(void)
{
Feb 20, 2019
Feb 20, 2019
336
337
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
338
339
340
341
devicelock_available_cancel();
log_debug("querying %s name owner", DEVICELOCK_SERVICE);
Aug 24, 2018
Aug 24, 2018
342
343
344
umdbus_get_name_owner_async(DEVICELOCK_SERVICE,
devicelock_available_cb,
&devicelock_available_pc);
Nov 7, 2016
Nov 7, 2016
345
346
}
Aug 24, 2018
Aug 24, 2018
347
static void devicelock_name_owner_signal(DBusMessage *msg)
Nov 7, 2016
Nov 7, 2016
348
{
Feb 20, 2019
Feb 20, 2019
349
350
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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)
{
Feb 20, 2019
Feb 20, 2019
379
380
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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) )
{
Aug 24, 2018
Aug 24, 2018
394
devicelock_name_owner_signal(msg);
Nov 7, 2016
Nov 7, 2016
395
396
397
398
399
400
401
402
403
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/* ========================================================================= *
* start/stop devicelock state tracking
* ========================================================================= */
Aug 24, 2018
Aug 24, 2018
404
405
bool
devicelock_start_listener(void)
Nov 7, 2016
Nov 7, 2016
406
{
Feb 20, 2019
Feb 20, 2019
407
408
LOG_REGISTER_CONTEXT;
Aug 24, 2018
Aug 24, 2018
409
bool ack = false;
Nov 7, 2016
Nov 7, 2016
410
411
412
413
log_debug("starting devicelock listener");
/* Get connection ref */
Aug 24, 2018
Aug 24, 2018
414
if( (devicelock_con = umdbus_get_connection()) == 0 )
Nov 7, 2016
Nov 7, 2016
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();
Aug 24, 2018
Aug 24, 2018
435
ack = true;
Mar 22, 2011
Mar 22, 2011
436
437
cleanup:
Nov 7, 2016
Nov 7, 2016
438
439
return ack;
Mar 22, 2011
Mar 22, 2011
440
441
}
Nov 7, 2016
Nov 7, 2016
442
void
Aug 24, 2018
Aug 24, 2018
443
devicelock_stop_listener(void)
Nov 7, 2016
Nov 7, 2016
444
{
Feb 20, 2019
Feb 20, 2019
445
446
LOG_REGISTER_CONTEXT;
Nov 7, 2016
Nov 7, 2016
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;
}
}