Skip to content

Latest commit

 

History

History
633 lines (521 loc) · 19.6 KB

usb_moded-udev.c

File metadata and controls

633 lines (521 loc) · 19.6 KB
 
May 16, 2011
May 16, 2011
1
/**
Aug 24, 2018
Aug 24, 2018
2
3
4
* @file usb_moded-udev.c
*
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
Feb 7, 2019
Feb 7, 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
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Tapio Rantala <ext-tapio.rantala@nokia.com>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Jarko Poutiainen <jarko.poutiainen@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
*/
May 16, 2011
May 16, 2011
29
Sep 5, 2018
Sep 5, 2018
30
#include "usb_moded-udev.h"
Apr 6, 2011
Apr 6, 2011
31
Aug 24, 2018
Aug 24, 2018
32
#include "usb_moded-config-private.h"
Sep 5, 2018
Sep 5, 2018
33
#include "usb_moded-control.h"
Sep 5, 2018
Sep 5, 2018
34
35
36
37
38
39
#include "usb_moded-dbus-private.h"
#include "usb_moded-log.h"
#include <string.h>
#include <libudev.h>
Mar 22, 2011
Mar 22, 2011
40
Aug 24, 2018
Aug 24, 2018
41
42
43
44
45
46
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- umudev -- */
Aug 24, 2018
Aug 24, 2018
47
48
static gboolean umudev_cable_state_timer_cb (gpointer aptr);
static void umudev_cable_state_stop_timer (void);
Aug 24, 2018
Aug 24, 2018
49
static void umudev_cable_state_start_timer(gint delay);
Aug 24, 2018
Aug 24, 2018
50
51
52
53
54
55
static bool umudev_cable_state_connected (void);
static cable_state_t umudev_cable_state_get (void);
static void umudev_cable_state_set (cable_state_t state);
static void umudev_cable_state_changed (void);
static void umudev_cable_state_from_udev (cable_state_t state);
Aug 24, 2018
Aug 24, 2018
56
57
58
59
60
61
62
63
64
65
66
static void umudev_io_error_cb (gpointer data);
static gboolean umudev_io_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data);
static void umudev_parse_properties (struct udev_device *dev, bool initial);
static int umudev_score_as_power_supply(const char *syspath);
gboolean umudev_init (void);
void umudev_quit (void);
/* ========================================================================= *
* Data
* ========================================================================= */
Mar 22, 2011
Mar 22, 2011
67
/* global variables */
Aug 24, 2018
Aug 24, 2018
68
69
70
71
72
static struct udev *umudev_object = 0;
static struct udev_monitor *umudev_monitor = 0;
static gchar *umudev_sysname = 0;
static guint umudev_watch_id = 0;
static bool umudev_in_cleanup = false;
Mar 28, 2018
Mar 28, 2018
73
Aug 24, 2018
Aug 24, 2018
74
/** Cable state as evaluated from udev events */
Aug 24, 2018
Aug 24, 2018
75
static cable_state_t umudev_cable_state_current = CABLE_STATE_UNKNOWN;
Aug 17, 2011
Aug 17, 2011
76
Aug 24, 2018
Aug 24, 2018
77
/** Cable state considered active by usb-moded */
Aug 24, 2018
Aug 24, 2018
78
static cable_state_t umudev_cable_state_active = CABLE_STATE_UNKNOWN;
Dec 17, 2014
Dec 17, 2014
79
Aug 24, 2018
Aug 24, 2018
80
/** Previously active cable state */
Aug 24, 2018
Aug 24, 2018
81
static cable_state_t umudev_cable_state_previous = CABLE_STATE_UNKNOWN;
Dec 17, 2014
Dec 17, 2014
82
Aug 24, 2018
Aug 24, 2018
83
/** Timer id for delaying: reported by udev -> active in usb-moded */
Aug 24, 2018
Aug 24, 2018
84
static guint umudev_cable_state_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
85
static gint umudev_cable_state_timer_delay = -1;
Dec 17, 2014
Dec 17, 2014
86
Aug 24, 2018
Aug 24, 2018
87
88
89
/* ========================================================================= *
* cable state
* ========================================================================= */
Dec 17, 2014
Dec 17, 2014
90
Aug 24, 2018
Aug 24, 2018
91
static gboolean umudev_cable_state_timer_cb(gpointer aptr)
Mar 22, 2011
Mar 22, 2011
92
{
Aug 24, 2018
Aug 24, 2018
93
(void)aptr;
Aug 24, 2018
Aug 24, 2018
94
umudev_cable_state_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
95
umudev_cable_state_timer_delay = -1;
Aug 24, 2018
Aug 24, 2018
96
97
log_debug("trigger delayed transfer to: %s",
Aug 24, 2018
Aug 24, 2018
98
99
cable_state_repr(umudev_cable_state_current));
umudev_cable_state_set(umudev_cable_state_current);
Apr 20, 2011
Apr 20, 2011
100
return FALSE;
Aug 24, 2018
Aug 24, 2018
101
102
}
Aug 24, 2018
Aug 24, 2018
103
static void umudev_cable_state_stop_timer(void)
Aug 24, 2018
Aug 24, 2018
104
{
Aug 24, 2018
Aug 24, 2018
105
if( umudev_cable_state_timer_id ) {
Aug 24, 2018
Aug 24, 2018
106
log_debug("cancel delayed transfer to: %s",
Aug 24, 2018
Aug 24, 2018
107
108
109
cable_state_repr(umudev_cable_state_current));
g_source_remove(umudev_cable_state_timer_id),
umudev_cable_state_timer_id = 0;
Aug 24, 2018
Aug 24, 2018
110
umudev_cable_state_timer_delay = -1;
Dec 17, 2014
Dec 17, 2014
111
}
Aug 24, 2018
Aug 24, 2018
112
113
}
Aug 24, 2018
Aug 24, 2018
114
static void umudev_cable_state_start_timer(gint delay)
Aug 24, 2018
Aug 24, 2018
115
{
Aug 24, 2018
Aug 24, 2018
116
117
118
119
if( umudev_cable_state_timer_delay != delay ) {
umudev_cable_state_stop_timer();
}
Aug 24, 2018
Aug 24, 2018
120
if( !umudev_cable_state_timer_id ) {
Aug 24, 2018
Aug 24, 2018
121
log_debug("schedule delayed transfer to: %s",
Aug 24, 2018
Aug 24, 2018
122
123
cable_state_repr(umudev_cable_state_current));
umudev_cable_state_timer_id =
Aug 24, 2018
Aug 24, 2018
124
g_timeout_add(delay,
Aug 24, 2018
Aug 24, 2018
125
umudev_cable_state_timer_cb, 0);
Aug 24, 2018
Aug 24, 2018
126
umudev_cable_state_timer_delay = delay;
Dec 17, 2014
Dec 17, 2014
127
}
Aug 24, 2018
Aug 24, 2018
128
129
130
}
static bool
Aug 24, 2018
Aug 24, 2018
131
umudev_cable_state_connected(void)
Aug 24, 2018
Aug 24, 2018
132
133
{
bool connected = false;
Aug 24, 2018
Aug 24, 2018
134
switch( umudev_cable_state_get() ) {
Aug 24, 2018
Aug 24, 2018
135
136
137
138
139
140
default:
break;
case CABLE_STATE_CHARGER_CONNECTED:
case CABLE_STATE_PC_CONNECTED:
connected = true;
break;
Dec 17, 2014
Dec 17, 2014
141
}
Aug 24, 2018
Aug 24, 2018
142
return connected;
Apr 6, 2011
Apr 6, 2011
143
}
Mar 22, 2011
Mar 22, 2011
144
Aug 24, 2018
Aug 24, 2018
145
static cable_state_t umudev_cable_state_get(void)
Apr 6, 2011
Apr 6, 2011
146
{
Aug 24, 2018
Aug 24, 2018
147
return umudev_cable_state_active;
Aug 24, 2018
Aug 24, 2018
148
149
}
Aug 24, 2018
Aug 24, 2018
150
static void umudev_cable_state_set(cable_state_t state)
Aug 24, 2018
Aug 24, 2018
151
{
Aug 24, 2018
Aug 24, 2018
152
umudev_cable_state_stop_timer();
Apr 20, 2011
Apr 20, 2011
153
Aug 24, 2018
Aug 24, 2018
154
if( umudev_cable_state_active == state )
Aug 24, 2018
Aug 24, 2018
155
goto EXIT;
Apr 26, 2016
Apr 26, 2016
156
Aug 24, 2018
Aug 24, 2018
157
158
umudev_cable_state_previous = umudev_cable_state_active;
umudev_cable_state_active = state;
Apr 26, 2016
Apr 26, 2016
159
Aug 24, 2018
Aug 24, 2018
160
log_debug("cable_state: %s -> %s",
Aug 24, 2018
Aug 24, 2018
161
162
cable_state_repr(umudev_cable_state_previous),
cable_state_repr(umudev_cable_state_active));
Aug 24, 2018
Aug 24, 2018
163
Aug 24, 2018
Aug 24, 2018
164
umudev_cable_state_changed();
Aug 24, 2018
Aug 24, 2018
165
166
167
168
169
EXIT:
return;
}
Aug 24, 2018
Aug 24, 2018
170
static void umudev_cable_state_changed(void)
Aug 24, 2018
Aug 24, 2018
171
172
173
174
175
176
177
178
{
/* The rest of usb-moded separates charger
* and pc connection states... make single
* state tracking compatible with that. */
/* First handle pc/charger disconnect based
* on previous state.
*/
Aug 24, 2018
Aug 24, 2018
179
switch( umudev_cable_state_previous ) {
Aug 24, 2018
Aug 24, 2018
180
181
182
183
184
default:
case CABLE_STATE_DISCONNECTED:
/* dontcare */
break;
case CABLE_STATE_CHARGER_CONNECTED:
Feb 7, 2019
Feb 7, 2019
185
umdbus_send_event_signal(CHARGER_DISCONNECTED);
Aug 24, 2018
Aug 24, 2018
186
187
break;
case CABLE_STATE_PC_CONNECTED:
Feb 7, 2019
Feb 7, 2019
188
umdbus_send_event_signal(USB_DISCONNECTED);
Aug 24, 2018
Aug 24, 2018
189
break;
Apr 26, 2016
Apr 26, 2016
190
}
Aug 24, 2018
Aug 24, 2018
191
Aug 24, 2018
Aug 24, 2018
192
/* Then handle pc/charger connect based
Aug 24, 2018
Aug 24, 2018
193
194
195
* on current state.
*/
Aug 24, 2018
Aug 24, 2018
196
switch( umudev_cable_state_active ) {
Aug 24, 2018
Aug 24, 2018
197
198
199
200
201
default:
case CABLE_STATE_DISCONNECTED:
/* dontcare */
break;
case CABLE_STATE_CHARGER_CONNECTED:
Feb 7, 2019
Feb 7, 2019
202
umdbus_send_event_signal(CHARGER_CONNECTED);
Aug 24, 2018
Aug 24, 2018
203
204
break;
case CABLE_STATE_PC_CONNECTED:
Feb 7, 2019
Feb 7, 2019
205
umdbus_send_event_signal(USB_CONNECTED);
Aug 24, 2018
Aug 24, 2018
206
break;
Mar 22, 2011
Mar 22, 2011
207
}
Aug 24, 2018
Aug 24, 2018
208
209
/* Then act on usb mode */
Sep 5, 2018
Sep 5, 2018
210
control_set_cable_state(umudev_cable_state_active);
Aug 24, 2018
Aug 24, 2018
211
212
}
Aug 24, 2018
Aug 24, 2018
213
static void umudev_cable_state_from_udev(cable_state_t curr)
Aug 24, 2018
Aug 24, 2018
214
{
Aug 24, 2018
Aug 24, 2018
215
216
cable_state_t prev = umudev_cable_state_current;
umudev_cable_state_current = curr;
Apr 26, 2016
Apr 26, 2016
217
Aug 24, 2018
Aug 24, 2018
218
219
if( prev == curr )
goto EXIT;
Apr 26, 2016
Apr 26, 2016
220
Aug 24, 2018
Aug 24, 2018
221
log_debug("reported cable state: %s -> %s",
Aug 24, 2018
Aug 24, 2018
222
223
cable_state_repr(prev),
cable_state_repr(curr));
Apr 26, 2016
Apr 26, 2016
224
Aug 24, 2018
Aug 24, 2018
225
226
227
228
229
230
231
232
233
234
235
/* Because mode transitions are handled synchronously and can thus
* block the usb-moded mainloop, we might end up receiving a bursts
* of stale udev events after returning from mode switch - including
* multiple cable connect / disconnect events due to user replugging
* the cable in frustration of things taking too long.
*/
if( curr == CABLE_STATE_DISCONNECTED ) {
/* If we see any disconnect events, those must be acted on
* immediately to get the 1st disconnect handled.
*/
Aug 24, 2018
Aug 24, 2018
236
umudev_cable_state_set(curr);
Aug 24, 2018
Aug 24, 2018
237
238
239
240
241
242
243
244
245
}
else {
/* All other transitions are handled with at least 100 ms delay.
* This should compress multiple stale disconnect + connect
* pairs into single action.
*/
gint delay = 100;
if( curr == CABLE_STATE_PC_CONNECTED && prev != CABLE_STATE_UNKNOWN ) {
Sep 5, 2018
Sep 5, 2018
246
247
if( delay < usbmoded_get_cable_connection_delay() )
delay = usbmoded_get_cable_connection_delay();
Aug 24, 2018
Aug 24, 2018
248
249
250
251
}
umudev_cable_state_start_timer(delay);
}
Apr 26, 2016
Apr 26, 2016
252
Aug 24, 2018
Aug 24, 2018
253
254
EXIT:
return;
Mar 22, 2011
Mar 22, 2011
255
256
}
Aug 24, 2018
Aug 24, 2018
257
258
259
260
261
/* ========================================================================= *
* legacy code
* ========================================================================= */
static void umudev_io_error_cb(gpointer data)
Mar 22, 2011
Mar 22, 2011
262
{
Aug 24, 2018
Aug 24, 2018
263
264
265
266
267
268
269
270
271
(void)data;
/* we do not want to restart when we try to clean up */
if( !umudev_in_cleanup ) {
log_debug("USB connection watch destroyed, restarting it\n!");
/* restart trigger */
umudev_quit();
umudev_init();
}
Mar 22, 2011
Mar 22, 2011
272
273
}
Aug 24, 2018
Aug 24, 2018
274
static gboolean umudev_io_input_cb(GIOChannel *iochannel, GIOCondition cond, gpointer data)
Apr 22, 2016
Apr 22, 2016
275
{
Aug 24, 2018
Aug 24, 2018
276
277
278
279
280
(void)iochannel;
(void)data;
gboolean continue_watching = TRUE;
Sep 5, 2018
Sep 5, 2018
281
282
/* No code paths are allowed to bypass the common_release_wakelock() call below */
common_acquire_wakelock(USB_MODED_WAKELOCK_PROCESS_INPUT);
Aug 24, 2018
Aug 24, 2018
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
if( cond & G_IO_IN )
{
/* This normally blocks but G_IO_IN indicates that we can read */
struct udev_device *dev = udev_monitor_receive_device(umudev_monitor);
if( !dev )
{
/* if we get something else something bad happened stop watching to avoid busylooping */
continue_watching = FALSE;
}
else
{
/* check if it is the actual device we want to check */
if( !strcmp(umudev_sysname, udev_device_get_sysname(dev)) )
{
if( !strcmp(udev_device_get_action(dev), "change") )
{
umudev_parse_properties(dev, false);
}
}
udev_device_unref(dev);
}
}
if( cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) )
{
/* Unhandled errors turn io watch to virtual busyloop too */
continue_watching = FALSE;
}
if( !continue_watching && umudev_watch_id )
{
umudev_watch_id = 0;
log_crit("udev io watch disabled");
}
Apr 26, 2016
Apr 26, 2016
319
Sep 5, 2018
Sep 5, 2018
320
common_release_wakelock(USB_MODED_WAKELOCK_PROCESS_INPUT);
Apr 26, 2016
Apr 26, 2016
321
Aug 24, 2018
Aug 24, 2018
322
return continue_watching;
Apr 26, 2016
Apr 26, 2016
323
324
}
Aug 24, 2018
Aug 24, 2018
325
static void umudev_parse_properties(struct udev_device *dev, bool initial)
Apr 26, 2016
Apr 26, 2016
326
{
Aug 24, 2018
Aug 24, 2018
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
356
357
358
359
(void)initial;
/* udev properties we are interested in */
const char *power_supply_present = 0;
const char *power_supply_online = 0;
const char *power_supply_type = 0;
/* Assume there is no usb connection until proven otherwise */
bool connected = false;
/* Unless debug logging has been request via command line,
* suppress warnings about potential property issues and/or
* fallback strategies applied (to avoid spamming due to the
* code below seeing the same property values over and over
* again also in stable states).
*/
bool warnings = log_p(LOG_DEBUG);
/*
* Check for present first as some drivers use online for when charging
* is enabled
*/
power_supply_present = udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT");
if( !power_supply_present ) {
power_supply_present =
power_supply_online = udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE");
}
if( power_supply_present && !strcmp(power_supply_present, "1") )
connected = true;
/* Transition period = Connection status derived from udev
* events disagrees with usb-moded side bookkeeping. */
Sep 5, 2018
Sep 5, 2018
360
if( connected != control_get_connection_state() ) {
Aug 24, 2018
Aug 24, 2018
361
362
363
364
365
366
367
368
369
370
371
/* Enable udev property diagnostic logging */
warnings = true;
/* Block suspend briefly */
usbmoded_delay_suspend();
}
if( !connected ) {
/* Handle: Disconnected */
if( warnings && !power_supply_present )
log_err("No usable power supply indicator\n");
Aug 24, 2018
Aug 24, 2018
372
umudev_cable_state_from_udev(CABLE_STATE_DISCONNECTED);
Aug 24, 2018
Aug 24, 2018
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
}
else {
if( warnings && power_supply_online )
log_warning("Using online property\n");
/* At least h4113 i.e. "Xperia XA2 - Dual SIM" seem to have
* POWER_SUPPLY_REAL_TYPE udev property with information
* that usb-moded expects to be in POWER_SUPPLY_TYPE prop.
*/
power_supply_type = udev_device_get_property_value(dev, "POWER_SUPPLY_REAL_TYPE");
if( !power_supply_type )
power_supply_type = udev_device_get_property_value(dev, "POWER_SUPPLY_TYPE");
/*
* Power supply type might not exist also :(
* Send connected event but this will not be able
* to discriminate between charger/cable.
*/
if( !power_supply_type ) {
if( warnings )
log_warning("Fallback since cable detection might not be accurate. "
"Will connect on any voltage on charger.\n");
Aug 24, 2018
Aug 24, 2018
394
umudev_cable_state_from_udev(CABLE_STATE_PC_CONNECTED);
Aug 24, 2018
Aug 24, 2018
395
396
397
398
399
400
401
goto cleanup;
}
log_debug("CONNECTED - POWER_SUPPLY_TYPE = %s", power_supply_type);
if( !strcmp(power_supply_type, "USB") ||
!strcmp(power_supply_type, "USB_CDP") ) {
Aug 24, 2018
Aug 24, 2018
402
umudev_cable_state_from_udev(CABLE_STATE_PC_CONNECTED);
Aug 24, 2018
Aug 24, 2018
403
404
405
406
}
else if( !strcmp(power_supply_type, "USB_DCP") ||
!strcmp(power_supply_type, "USB_HVDCP") ||
!strcmp(power_supply_type, "USB_HVDCP_3") ) {
Aug 24, 2018
Aug 24, 2018
407
umudev_cable_state_from_udev(CABLE_STATE_CHARGER_CONNECTED);
Aug 24, 2018
Aug 24, 2018
408
409
}
else if( !strcmp(power_supply_type, "USB_FLOAT")) {
Aug 24, 2018
Aug 24, 2018
410
if( !umudev_cable_state_connected() )
Aug 24, 2018
Aug 24, 2018
411
log_warning("connection type detection failed, assuming charger");
Aug 24, 2018
Aug 24, 2018
412
umudev_cable_state_from_udev(CABLE_STATE_CHARGER_CONNECTED);
Aug 24, 2018
Aug 24, 2018
413
414
415
416
}
else if( !strcmp(power_supply_type, "Unknown")) {
// nop
log_warning("unknown connection type reported, assuming disconnected");
Aug 24, 2018
Aug 24, 2018
417
umudev_cable_state_from_udev(CABLE_STATE_DISCONNECTED);
Aug 24, 2018
Aug 24, 2018
418
419
420
421
}
else {
if( warnings )
log_warning("unhandled power supply type: %s", power_supply_type);
Aug 24, 2018
Aug 24, 2018
422
umudev_cable_state_from_udev(CABLE_STATE_DISCONNECTED);
Aug 24, 2018
Aug 24, 2018
423
424
425
426
427
}
}
cleanup:
return;
Apr 26, 2016
Apr 26, 2016
428
429
}
Aug 24, 2018
Aug 24, 2018
430
static int umudev_score_as_power_supply(const char *syspath)
Apr 26, 2016
Apr 26, 2016
431
{
Aug 24, 2018
Aug 24, 2018
432
433
434
435
436
437
int score = 0;
struct udev_device *dev = 0;
const char *sysname = 0;
if( !umudev_object )
goto EXIT;
Mar 28, 2018
Mar 28, 2018
438
Aug 24, 2018
Aug 24, 2018
439
440
if( !(dev = udev_device_new_from_syspath(umudev_object, syspath)) )
goto EXIT;
Apr 26, 2016
Apr 26, 2016
441
Aug 24, 2018
Aug 24, 2018
442
443
444
445
446
447
448
449
450
451
452
453
if( !(sysname = udev_device_get_sysname(dev)) )
goto EXIT;
/* try to assign a weighed score */
/* check that it is not a battery */
if(strstr(sysname, "battery") || strstr(sysname, "BAT"))
goto EXIT;
/* if it contains usb in the name it very likely is good */
if(strstr(sysname, "usb"))
score = score + 10;
Apr 26, 2016
Apr 26, 2016
454
Aug 24, 2018
Aug 24, 2018
455
456
457
458
459
460
461
462
463
464
465
466
/* often charger is also mentioned in the name */
if(strstr(sysname, "charger"))
score = score + 5;
/* present property is used to detect activity, however online is better */
if(udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT"))
score = score + 5;
if(udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE"))
score = score + 10;
/* type is used to detect if it is a cable or dedicated charger.
Aug 24, 2018
Aug 24, 2018
467
* Bonus points if it is there. */
Aug 24, 2018
Aug 24, 2018
468
469
470
471
472
473
474
475
476
if(udev_device_get_property_value(dev, "POWER_SUPPLY_TYPE"))
score = score + 10;
EXIT:
/* clean up */
if( dev )
udev_device_unref(dev);
return score;
Apr 22, 2016
Apr 22, 2016
477
478
}
Aug 24, 2018
Aug 24, 2018
479
gboolean umudev_init(void)
Apr 12, 2011
Apr 12, 2011
480
{
Aug 24, 2018
Aug 24, 2018
481
gboolean success = FALSE;
Apr 26, 2016
Apr 26, 2016
482
Aug 24, 2018
Aug 24, 2018
483
484
485
char *configured_device = NULL;
char *configured_subsystem = NULL;
struct udev_device *dev = 0;
Sep 5, 2018
Sep 5, 2018
486
GIOChannel *iochannel = 0;
Apr 26, 2016
Apr 26, 2016
487
Aug 24, 2018
Aug 24, 2018
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
int ret = 0;
/* Clear in-cleanup in case of restart */
umudev_in_cleanup = false;
/* Create the udev object */
if( !(umudev_object = udev_new()) ) {
log_err("Can't create umudev_object\n");
goto EXIT;
}
if( !(configured_device = config_find_udev_path()) )
configured_device = g_strdup("/sys/class/power_supply/usb");
if( !(configured_subsystem = config_find_udev_subsystem()) )
configured_subsystem = g_strdup("power_supply");
/* Try with configured / default device */
dev = udev_device_new_from_syspath(umudev_object, configured_device);
/* If needed, try heuristics */
if( !dev ) {
log_debug("Trying to guess $power_supply device.\n");
int current_score = 0;
gchar *current_name = 0;
struct udev_enumerate *list;
struct udev_list_entry *list_entry;
struct udev_list_entry *first_entry;
list = udev_enumerate_new(umudev_object);
udev_enumerate_add_match_subsystem(list, "power_supply");
udev_enumerate_scan_devices(list);
first_entry = udev_enumerate_get_list_entry(list);
udev_list_entry_foreach(list_entry, first_entry) {
const char *name = udev_list_entry_get_name(list_entry);
int score = umudev_score_as_power_supply(name);
if( current_score < score ) {
g_free(current_name);
current_name = g_strdup(name);
current_score = score;
}
}
/* check if we found anything with some kind of score */
if(current_score > 0) {
dev = udev_device_new_from_syspath(umudev_object, current_name);
}
g_free(current_name);
}
/* Give up if no power supply device was found */
if( !dev ) {
log_err("Unable to find $power_supply device.");
/* communicate failure, mainloop will exit and call appropriate clean-up */
goto EXIT;
}
/* Cache device name */
umudev_sysname = g_strdup(udev_device_get_sysname(dev));
log_debug("device name = %s\n", umudev_sysname);
/* Start monitoring for changes */
umudev_monitor = udev_monitor_new_from_netlink(umudev_object, "udev");
if( !umudev_monitor )
{
log_err("Unable to monitor the netlink\n");
/* communicate failure, mainloop will exit and call appropriate clean-up */
goto EXIT;
}
ret = udev_monitor_filter_add_match_subsystem_devtype(umudev_monitor,
configured_subsystem,
NULL);
if(ret != 0)
{
log_err("Udev match failed.\n");
goto EXIT;
}
ret = udev_monitor_enable_receiving(umudev_monitor);
if(ret != 0)
{
log_err("Failed to enable monitor recieving.\n");
goto EXIT;
}
iochannel = g_io_channel_unix_new(udev_monitor_get_fd(umudev_monitor));
if( !iochannel )
goto EXIT;
umudev_watch_id = g_io_add_watch_full(iochannel, 0, G_IO_IN, umudev_io_input_cb, NULL, umudev_io_error_cb);
if( !umudev_watch_id )
goto EXIT;
/* everything went well */
success = TRUE;
/* check initial status */
umudev_parse_properties(dev, true);
EXIT:
/* Cleanup local resources */
if( iochannel )
g_io_channel_unref(iochannel);
if( dev )
udev_device_unref(dev);
g_free(configured_subsystem);
g_free(configured_device);
/* All or nothing */
if( !success )
umudev_quit();
return success;
Apr 26, 2016
Apr 26, 2016
605
606
}
Aug 24, 2018
Aug 24, 2018
607
void umudev_quit(void)
Apr 26, 2016
Apr 26, 2016
608
{
Aug 24, 2018
Aug 24, 2018
609
umudev_in_cleanup = true;
Apr 26, 2016
Apr 26, 2016
610
Aug 24, 2018
Aug 24, 2018
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
log_debug("HWhal cleanup\n");
if( umudev_watch_id )
{
g_source_remove(umudev_watch_id),
umudev_watch_id = 0;
}
if( umudev_monitor ) {
udev_monitor_unref(umudev_monitor),
umudev_monitor = 0;
}
if( umudev_object ) {
udev_unref(umudev_object),
umudev_object =0 ;
}
g_free(umudev_sysname),
umudev_sysname = 0;
Aug 24, 2018
Aug 24, 2018
632
umudev_cable_state_stop_timer();
Apr 12, 2011
Apr 12, 2011
633
}