Skip to content

Latest commit

 

History

History
1026 lines (888 loc) · 25.6 KB

usb_moded.c

File metadata and controls

1026 lines (888 loc) · 25.6 KB
 
Mar 22, 2011
Mar 22, 2011
1
2
3
4
/**
@file usb_moded.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
Dec 6, 2012
Dec 6, 2012
5
Copyright (C) 2012 Jolla. All rights reserved.
Mar 22, 2011
Mar 22, 2011
6
7
@author: Philippe De Swert <philippe.de-swert@nokia.com>
Dec 6, 2012
Dec 6, 2012
8
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
Mar 22, 2011
Mar 22, 2011
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
*/
#define _GNU_SOURCE
#include <getopt.h>
Mar 4, 2013
Mar 4, 2013
27
#include <stdio.h>
Mar 22, 2011
Mar 22, 2011
28
29
30
#include <sys/stat.h>
#include <sys/wait.h>
Oct 22, 2013
Oct 22, 2013
31
#include <signal.h>
Mar 22, 2011
Mar 22, 2011
32
Nov 13, 2012
Nov 13, 2012
33
#include <libkmod.h>
Jan 20, 2014
Jan 20, 2014
34
35
36
#ifdef SYSTEMD
#include <systemd/sd-daemon.h>
#endif
Nov 13, 2012
Nov 13, 2012
37
Mar 22, 2011
Mar 22, 2011
38
39
40
41
42
43
44
#include "usb_moded.h"
#include "usb_moded-modes.h"
#include "usb_moded-dbus.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-hw-ab.h"
#include "usb_moded-modules.h"
#include "usb_moded-log.h"
Mar 1, 2012
Mar 1, 2012
45
#include "usb_moded-lock.h"
Mar 22, 2011
Mar 22, 2011
46
47
48
#include "usb_moded-modesetting.h"
#include "usb_moded-modules.h"
#include "usb_moded-appsync.h"
May 16, 2011
May 16, 2011
49
50
#include "usb_moded-trigger.h"
#include "usb_moded-config.h"
Sep 11, 2011
Sep 11, 2011
51
#include "usb_moded-config-private.h"
Jan 13, 2012
Jan 13, 2012
52
#include "usb_moded-network.h"
Jan 17, 2013
Jan 17, 2013
53
#include "usb_moded-mac.h"
Aug 2, 2013
Aug 2, 2013
54
#include "usb_moded-android.h"
Aug 21, 2013
Aug 21, 2013
55
56
57
#ifdef MEEGOLOCK
#include "usb_moded-dsme.h"
#endif
Mar 22, 2011
Mar 22, 2011
58
59
60
/* global definitions */
Oct 22, 2013
Oct 22, 2013
61
GMainLoop *mainloop = NULL;
Mar 22, 2011
Mar 22, 2011
62
63
64
65
extern const char *log_name;
extern int log_level;
extern int log_type;
Sep 10, 2013
Sep 10, 2013
66
67
gboolean rescue_mode = FALSE;
gboolean diag_mode = FALSE;
Oct 2, 2012
Oct 2, 2012
68
gboolean hw_fallback = FALSE;
Jun 2, 2014
Jun 2, 2014
69
gboolean android_broken_usb = FALSE;
Apr 19, 2015
Apr 19, 2015
70
71
gboolean android_ignore_udev_events = FALSE;
gboolean android_ignore_next_udev_disconnect_event = FALSE;
Jan 20, 2014
Jan 20, 2014
72
73
74
#ifdef SYSTEMD
static gboolean systemd_notify = FALSE;
#endif
Sep 10, 2013
Sep 10, 2013
75
Mar 22, 2011
Mar 22, 2011
76
struct usb_mode current_mode;
Jul 14, 2011
Jul 14, 2011
77
guint charging_timeout = 0;
May 10, 2011
May 10, 2011
78
static GList *modelist;
Mar 22, 2011
Mar 22, 2011
79
80
/* static helper functions */
May 17, 2011
May 17, 2011
81
static gboolean set_disconnected(gpointer data);
Apr 30, 2014
Apr 30, 2014
82
static gboolean set_disconnected_silent(gpointer data);
Mar 22, 2011
Mar 22, 2011
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static void usb_moded_init(void);
static gboolean charging_fallback(gpointer data);
static void usage(void);
/* ============= Implementation starts here =========================================== */
/** set the usb connection status
*
* @param connected The connection status, true for connected
*
*/
void set_usb_connected(gboolean connected)
{
if(connected)
{
/* do not go through the routine if already connected to avoid
spurious load/unloads due to faulty signalling
NOKIA: careful with devicelock
*/
Jan 12, 2015
Jan 12, 2015
102
if(current_mode.connected == connected)
Mar 22, 2011
Mar 22, 2011
103
return;
May 17, 2011
May 17, 2011
104
Jul 14, 2011
Jul 14, 2011
105
106
107
108
109
if(charging_timeout)
{
g_source_remove(charging_timeout);
charging_timeout = 0;
}
Mar 22, 2011
Mar 22, 2011
110
current_mode.connected = TRUE;
Sep 30, 2013
Sep 30, 2013
111
112
113
/* signal usb connected */
log_debug("usb connected\n");
usb_moded_send_signal(USB_CONNECTED);
Mar 22, 2011
Mar 22, 2011
114
115
116
set_usb_connected_state();
}
else
Jun 27, 2011
Jun 27, 2011
117
{
Oct 8, 2013
Oct 8, 2013
118
119
if(current_mode.connected == FALSE)
return;
Apr 19, 2015
Apr 19, 2015
120
121
122
123
if(android_ignore_next_udev_disconnect_event) {
android_ignore_next_udev_disconnect_event = FALSE;
return;
}
Jun 27, 2011
Jun 27, 2011
124
125
current_mode.connected = FALSE;
set_disconnected(NULL);
Jun 2, 2014
Jun 2, 2014
126
127
128
129
130
131
/* Some android kernels check for an active gadget to enable charging and
* cable detection, meaning USB is completely dead unless we keep the gadget
* active
*/
if(current_mode.android_usb_broken)
set_android_charging_mode();
May 23, 2015
May 23, 2015
132
133
134
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
Jun 27, 2011
Jun 27, 2011
135
}
May 17, 2011
May 17, 2011
136
137
138
139
140
}
static gboolean set_disconnected(gpointer data)
{
Nov 7, 2012
Nov 7, 2012
141
142
/* let usb settle */
sleep(1);
Jun 27, 2011
Jun 27, 2011
143
144
145
146
/* only disconnect for real if we are actually still disconnected */
if(!get_usb_connection_state())
{
log_debug("usb disconnected\n");
Jul 21, 2011
Jul 21, 2011
147
148
/* signal usb disconnected */
usb_moded_send_signal(USB_DISCONNECTED);
Oct 8, 2013
Oct 8, 2013
149
/* unload modules and general cleanup if not charging */
Aug 26, 2014
Aug 26, 2014
150
151
if(strcmp(get_usb_mode(), MODE_CHARGING) ||
strcmp(get_usb_mode(), MODE_CHARGING_FALLBACK))
Aug 8, 2012
Aug 8, 2012
152
usb_moded_mode_cleanup(get_usb_module());
Nov 7, 2012
Nov 7, 2012
153
/* Nothing else as we do not need to do anything for cleaning up charging mode */
Jun 27, 2011
Jun 27, 2011
154
usb_moded_module_cleanup(get_usb_module());
Jul 15, 2011
Jul 15, 2011
155
set_usb_mode(MODE_UNDEFINED);
Mar 22, 2011
Mar 22, 2011
156
Jun 27, 2011
Jun 27, 2011
157
158
}
return FALSE;
Mar 22, 2011
Mar 22, 2011
159
160
}
Apr 30, 2014
Apr 30, 2014
161
162
163
164
165
166
167
/* set disconnected without sending signals. */
static gboolean set_disconnected_silent(gpointer data)
{
if(!get_usb_connection_state())
{
log_debug("Resetting connection data after HUP\n");
/* unload modules and general cleanup if not charging */
Aug 26, 2014
Aug 26, 2014
168
169
if(strcmp(get_usb_mode(), MODE_CHARGING) ||
strcmp(get_usb_mode(), MODE_CHARGING_FALLBACK))
Apr 30, 2014
Apr 30, 2014
170
171
172
173
174
175
176
177
178
usb_moded_mode_cleanup(get_usb_module());
/* Nothing else as we do not need to do anything for cleaning up charging mode */
usb_moded_module_cleanup(get_usb_module());
set_usb_mode(MODE_UNDEFINED);
}
return FALSE;
}
May 10, 2013
May 10, 2013
179
180
181
182
183
/** set and track charger state
*
*/
void set_charger_connected(gboolean state)
{
Dec 4, 2014
Dec 4, 2014
184
185
/* check if charger is already connected
to avoid spamming dbus */
Jan 12, 2015
Jan 12, 2015
186
if(current_mode.connected == state)
Dec 4, 2014
Dec 4, 2014
187
188
return;
May 10, 2013
May 10, 2013
189
190
191
192
if(state)
{
usb_moded_send_signal(CHARGER_CONNECTED);
set_usb_mode(MODE_CHARGER);
Dec 4, 2014
Dec 4, 2014
193
current_mode.connected = TRUE;
May 10, 2013
May 10, 2013
194
195
196
}
else
{
Sep 25, 2013
Sep 25, 2013
197
current_mode.connected = FALSE;
May 10, 2013
May 10, 2013
198
199
usb_moded_send_signal(CHARGER_DISCONNECTED);
set_usb_mode(MODE_UNDEFINED);
Dec 4, 2014
Dec 4, 2014
200
current_mode.connected = FALSE;
May 10, 2013
May 10, 2013
201
202
203
}
}
Mar 22, 2011
Mar 22, 2011
204
205
206
207
208
/** set the chosen usb state
*
*/
void set_usb_connected_state(void)
{
Nov 11, 2014
Nov 11, 2014
209
char *mode_to_set;
Mar 1, 2012
Mar 1, 2012
210
#ifdef MEEGOLOCK
Feb 15, 2013
Feb 15, 2013
211
int export = 1; /* assume locked */
Mar 1, 2012
Mar 1, 2012
212
#endif /* MEEGOLOCK */
Mar 22, 2011
Mar 22, 2011
213
Feb 15, 2013
Feb 15, 2013
214
215
if(rescue_mode)
{
Feb 15, 2013
Feb 15, 2013
216
log_debug("Entering rescue mode!\n");
Feb 15, 2013
Feb 15, 2013
217
218
219
set_usb_mode(MODE_DEVELOPER);
return;
}
Oct 9, 2013
Oct 9, 2013
220
else if(diag_mode)
Sep 10, 2013
Sep 10, 2013
221
222
223
224
225
226
227
228
229
230
{
log_debug("Entering diagnostic mode!\n");
if(modelist)
{
GList *iter = modelist;
struct mode_list_elem *data = iter->data;
set_usb_mode(data->mode_name);
}
return;
}
Oct 9, 2013
Oct 9, 2013
231
232
233
mode_to_set = get_mode_setting();
Mar 1, 2012
Mar 1, 2012
234
#ifdef MEEGOLOCK
Sep 13, 2013
Sep 13, 2013
235
236
/* check if we are allowed to export system contents 0 is unlocked */
export = usb_moded_get_export_permission();
Aug 21, 2013
Aug 21, 2013
237
238
239
/* We check also if the device is in user state or not.
If not we do not export anything. We presume ACT_DEAD charging */
if(mode_to_set && !export && is_in_user_state())
Mar 22, 2011
Mar 22, 2011
240
#else
Mar 1, 2012
Mar 1, 2012
241
if(mode_to_set)
Mar 1, 2012
Mar 1, 2012
242
#endif /* MEEGOLOCK */
Mar 22, 2011
Mar 22, 2011
243
{
Aug 5, 2014
Aug 5, 2014
244
245
246
247
248
249
/* This is safe to do here as the starting condition is
MODE_UNDEFINED, and having a devicelock being activated when
a mode is set will not interrupt it */
if(!strcmp(mode_to_set, current_mode.mode))
goto end;
Mar 22, 2011
Mar 22, 2011
250
251
252
253
254
255
256
if(!strcmp(MODE_ASK, mode_to_set))
{
/* send signal, mode will be set when the dialog service calls
the set_mode method call.
*/
usb_moded_send_signal(USB_CONNECTED_DIALOG_SHOW);
/* fallback to charging mode after 3 seconds */
Jul 14, 2011
Jul 14, 2011
257
charging_timeout = g_timeout_add_seconds(3, charging_fallback, NULL);
Mar 22, 2011
Mar 22, 2011
258
259
260
261
262
263
264
265
266
/* in case there was nobody listening for the UI, they will know
that the UI is needed by requesting the current mode */
set_usb_mode(MODE_ASK);
}
else
set_usb_mode(mode_to_set);
}
else
{
Aug 14, 2013
Aug 14, 2013
267
/* config is corrupted or we do not have a mode configured, fallback to charging
Mar 22, 2011
Mar 22, 2011
268
269
270
We also fall back here in case the device is locked and we do not
export the system contents. Or if we are in acting dead mode.
*/
Aug 26, 2014
Aug 26, 2014
271
set_usb_mode(MODE_CHARGING_FALLBACK);
Mar 22, 2011
Mar 22, 2011
272
}
Aug 5, 2014
Aug 5, 2014
273
end:
Nov 11, 2014
Nov 11, 2014
274
free(mode_to_set);
Mar 22, 2011
Mar 22, 2011
275
276
277
278
279
280
281
282
283
}
/** set the usb mode
*
* @param mode The requested USB mode
*
*/
void set_usb_mode(const char *mode)
{
Jan 13, 2012
Jan 13, 2012
284
285
/* set return to 1 to be sure to error out if no matching mode is found either */
int ret=1, net=0;
Jun 19, 2013
Jun 19, 2013
286
Jan 14, 2014
Jan 14, 2014
287
288
289
#ifdef MEEGOLOCK
/* Do a second check in case timer suspend causes a race issue */
int export = 1;
Aug 26, 2014
Aug 26, 2014
290
#endif
Jan 14, 2014
Jan 14, 2014
291
Dec 10, 2014
Dec 10, 2014
292
293
log_debug("Setting %s\n", mode);
Aug 26, 2014
Aug 26, 2014
294
295
/* CHARGING AND FALLBACK CHARGING are always ok to set, so this can be done
before the optional second device lock check */
Aug 26, 2014
Aug 26, 2014
296
if(!strcmp(mode, MODE_CHARGING) || !strcmp(mode, MODE_CHARGING_FALLBACK))
Mar 22, 2011
Mar 22, 2011
297
298
299
300
301
302
{
check_module_state(MODULE_MASS_STORAGE);
/* for charging we use a fake file_storage (blame USB certification for this insanity */
set_usb_module(MODULE_MASS_STORAGE);
/* MODULE_CHARGING has all the parameters defined, so it will not match the g_file_storage rule in usb_moded_load_module */
ret = usb_moded_load_module(MODULE_CHARGING);
Aug 14, 2013
Aug 14, 2013
303
304
/* if charging mode setting did not succeed we might be dealing with android */
if(ret)
Nov 29, 2013
Nov 29, 2013
305
{
Apr 19, 2015
Apr 19, 2015
306
307
308
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
Nov 29, 2013
Nov 29, 2013
309
set_usb_module(MODULE_NONE);
Aug 14, 2013
Aug 14, 2013
310
ret = set_android_charging_mode();
Nov 29, 2013
Nov 29, 2013
311
}
Mar 22, 2011
Mar 22, 2011
312
313
goto end;
}
Aug 26, 2014
Aug 26, 2014
314
Dec 10, 2014
Dec 10, 2014
315
316
317
318
319
320
321
/* Dedicated charger mode needs nothing to be done and no user interaction */
if(!strcmp(mode, MODE_CHARGER))
{
ret = 0;
goto end;
}
Aug 26, 2014
Aug 26, 2014
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifdef MEEGOLOCK
/* check if we are allowed to export system contents 0 is unlocked */
/* In ACTDEAD export is always ok */
if(is_in_user_state())
{
export = usb_moded_get_export_permission();
if(export && !rescue_mode)
{
log_debug("Secondary device lock check failed. Not setting mode!\n");
goto end;
}
}
#endif /* MEEGOLOCK */
Dec 10, 2014
Dec 10, 2014
337
338
339
/* nothing needs to be done for this mode but signalling.
Handled here to avoid issues with ask menu and devicelock */
if(!strcmp(mode, MODE_ASK))
Jul 21, 2012
Jul 21, 2012
340
341
{
ret = 0;
May 27, 2013
May 27, 2013
342
goto end;
Jul 21, 2012
Jul 21, 2012
343
}
Mar 22, 2011
Mar 22, 2011
344
May 10, 2011
May 10, 2011
345
346
347
348
349
350
351
352
353
354
/* go through all the dynamic modes if the modelist exists*/
if(modelist)
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name))
{
Jun 19, 2013
Jun 19, 2013
355
log_debug("Matching mode %s found.\n", mode);
May 10, 2011
May 10, 2011
356
357
358
check_module_state(data->mode_module);
set_usb_module(data->mode_module);
ret = usb_moded_load_module(data->mode_module);
Jun 19, 2013
Jun 19, 2013
359
360
/* set data before calling any of the dynamic mode functions
as they will use the get_usb_mode_data function */
May 27, 2013
May 27, 2013
361
set_usb_mode_data(data);
Oct 22, 2013
Oct 22, 2013
362
/* check if modules are ok before continuing */
Apr 19, 2015
Apr 19, 2015
363
364
365
366
if(!ret) {
if (android_ignore_udev_events) {
android_ignore_next_udev_disconnect_event = TRUE;
}
Oct 22, 2013
Oct 22, 2013
367
ret = set_dynamic_mode();
Apr 19, 2015
Apr 19, 2015
368
}
May 10, 2011
May 10, 2011
369
370
371
372
}
}
}
Mar 22, 2011
Mar 22, 2011
373
end:
May 27, 2013
May 27, 2013
374
375
/* if ret != 0 then usb_module loading failed
no mode matched or MODE_UNDEFINED was requested */
Mar 22, 2011
Mar 22, 2011
376
377
378
379
if(ret)
{
set_usb_module(MODULE_NONE);
mode = MODE_UNDEFINED;
May 27, 2013
May 27, 2013
380
set_usb_mode_data(NULL);
Jan 10, 2014
Jan 10, 2014
381
log_debug("mode setting failed or device disconnected, mode to set was = %s\n", mode);
Mar 22, 2011
Mar 22, 2011
382
}
Aug 15, 2012
Aug 15, 2012
383
384
if(net)
log_debug("Network setting failed!\n");
Oct 29, 2013
Oct 29, 2013
385
386
free(current_mode.mode);
current_mode.mode = strdup(mode);
Aug 26, 2014
Aug 26, 2014
387
388
389
390
391
/* CHARGING_FALLBACK is an internal mode not to be broadcasted outside */
if(!strcmp(mode, MODE_CHARGING_FALLBACK))
usb_moded_send_signal(MODE_CHARGING);
else
usb_moded_send_signal(get_usb_mode());
Mar 22, 2011
Mar 22, 2011
392
}
Mar 4, 2013
Mar 4, 2013
393
May 10, 2011
May 10, 2011
394
395
396
/** check if a given usb_mode exists
*
* @param mode The mode to look for
Oct 31, 2013
Oct 31, 2013
397
* @return 0 if mode exists, 1 if it does not exist
May 10, 2011
May 10, 2011
398
399
400
401
402
*
*/
int valid_mode(const char *mode)
{
Aug 26, 2014
Aug 26, 2014
403
404
405
/* MODE_ASK, MODE_CHARGER and MODE_CHARGING_FALLBACK are not modes that are settable seen their special 'internal' status
so we only check the modes that are announed outside. Only exception is the built in MODE_CHARGING */
if(!strcmp(MODE_CHARGING, mode))
May 10, 2011
May 10, 2011
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
return(0);
else
{
/* check dynamic modes */
if(modelist)
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name))
return(0);
}
}
}
return(1);
}
Mar 22, 2011
Mar 22, 2011
425
Mar 4, 2013
Mar 4, 2013
426
427
428
429
430
/** make a list of all available usb modes
*
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
*
*/
May 27, 2013
May 27, 2013
431
gchar *get_mode_list(void)
Mar 4, 2013
Mar 4, 2013
432
433
{
May 27, 2013
May 27, 2013
434
GString *modelist_str;
Dec 15, 2015
Dec 15, 2015
435
char *hidden_modes_list;
Dec 26, 2015
Dec 26, 2015
436
gchar **hidden_mode_split = NULL;
Dec 15, 2015
Dec 15, 2015
437
438
439
440
441
442
443
444
int hiddenmode = 0, i;
hidden_modes_list = get_hidden_modes();
if(hidden_modes_list)
{
hidden_mode_split = g_strsplit(hidden_modes_list, ",", 0);
}
May 27, 2013
May 27, 2013
445
446
modelist_str = g_string_new(NULL);
Mar 4, 2013
Mar 4, 2013
447
Mar 21, 2014
Mar 21, 2014
448
if(!diag_mode)
Mar 4, 2013
Mar 4, 2013
449
450
{
/* check dynamic modes */
Mar 21, 2014
Mar 21, 2014
451
if(modelist)
Mar 4, 2013
Mar 4, 2013
452
453
454
455
456
457
{
GList *iter;
for( iter = modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
Mar 9, 2016
Mar 9, 2016
458
if(hidden_modes_list && hidden_mode_split)
Dec 15, 2015
Dec 15, 2015
459
460
461
462
463
464
465
466
467
468
469
for(i = 0; hidden_mode_split[i] != NULL; i++)
{
if(!strcmp(hidden_mode_split[i], data->mode_name))
hiddenmode = 1;
}
if(hiddenmode)
{
hiddenmode = 0;
continue;
}
May 27, 2013
May 27, 2013
470
modelist_str = g_string_append(modelist_str, data->mode_name);
Jun 19, 2013
Jun 19, 2013
471
modelist_str = g_string_append(modelist_str, ", ");
Mar 4, 2013
Mar 4, 2013
472
473
}
}
Mar 21, 2014
Mar 21, 2014
474
Mar 9, 2016
Mar 9, 2016
475
if(hidden_mode_split)
Dec 26, 2015
Dec 26, 2015
476
477
g_strfreev(hidden_mode_split);
Mar 21, 2014
Mar 21, 2014
478
479
480
481
482
483
484
485
486
/* end with charging mode */
g_string_append(modelist_str, MODE_CHARGING);
return(g_string_free(modelist_str, FALSE));
}
else
{
/* diag mode. there is only one active mode */
g_string_append(modelist_str, MODE_DIAG);
return(g_string_free(modelist_str, FALSE));
Mar 4, 2013
Mar 4, 2013
487
488
489
}
}
Mar 22, 2011
Mar 22, 2011
490
491
492
493
494
/** get the usb mode
*
* @return the currently set mode
*
*/
Jan 13, 2012
Jan 13, 2012
495
inline const char * get_usb_mode(void)
Mar 22, 2011
Mar 22, 2011
496
497
498
499
500
501
502
503
504
{
return(current_mode.mode);
}
/** set the loaded module
*
* @param module The module name for the requested mode
*
*/
Mar 30, 2013
Mar 30, 2013
505
void set_usb_module(const char *module)
Mar 22, 2011
Mar 22, 2011
506
{
Oct 29, 2013
Oct 29, 2013
507
508
free(current_mode.module);
current_mode.module = strdup(module);
Mar 22, 2011
Mar 22, 2011
509
510
511
512
513
514
515
}
/** get the supposedly loaded module
*
* @return The name of the loaded module
*
*/
Jan 13, 2012
Jan 13, 2012
516
inline const char * get_usb_module(void)
Mar 22, 2011
Mar 22, 2011
517
518
519
520
521
522
523
524
525
{
return(current_mode.module);
}
/** get if the cable is connected or not
*
* @ return A boolean value for connected (TRUE) or not (FALSE)
*
*/
Jan 13, 2012
Jan 13, 2012
526
inline gboolean get_usb_connection_state(void)
Mar 22, 2011
Mar 22, 2011
527
528
529
530
{
return current_mode.connected;
}
May 17, 2011
May 17, 2011
531
532
/** set connection status for some corner cases
*
Jun 27, 2011
Jun 27, 2011
533
* @param state The connection status that needs to be set. Connected (TRUE)
May 17, 2011
May 17, 2011
534
535
*
*/
Jan 13, 2012
Jan 13, 2012
536
inline void set_usb_connection_state(gboolean state)
May 17, 2011
May 17, 2011
537
538
539
{
current_mode.connected = state;
}
Mar 22, 2011
Mar 22, 2011
540
May 27, 2013
May 27, 2013
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/** set the mode_list_elem data
*
* @param data mode_list_element pointer
*
*/
void set_usb_mode_data(struct mode_list_elem *data)
{
current_mode.data = data;
}
/** get the usb mode data
*
* @return a pointer to the usb mode data
*
*/
inline struct mode_list_elem * get_usb_mode_data(void)
{
return(current_mode.data);
}
Mar 22, 2011
Mar 22, 2011
561
562
563
564
565
566
567
/*================ Static functions ===================================== */
/* set default values for usb_moded */
static void usb_moded_init(void)
{
current_mode.connected = FALSE;
current_mode.mounted = FALSE;
Oct 29, 2013
Oct 29, 2013
568
569
current_mode.mode = strdup(MODE_UNDEFINED);
current_mode.module = strdup(MODULE_NONE);
Mar 22, 2011
Mar 22, 2011
570
Jun 2, 2014
Jun 2, 2014
571
572
573
if(android_broken_usb)
current_mode.android_usb_broken = TRUE;
Mar 31, 2013
Mar 31, 2013
574
575
576
577
578
579
580
/* check config, merge or create if outdated */
if(conf_file_merge() != 0)
{
log_err("Cannot create or find a valid configuration. Exiting.\n");
exit(1);
}
Mar 22, 2011
Mar 22, 2011
581
#ifdef APP_SYNC
Mar 10, 2014
Mar 10, 2014
582
readlist(diag_mode);
Dec 15, 2013
Dec 15, 2013
583
584
/* make sure all services are down when starting */
appsync_stop();
Sep 10, 2013
Sep 10, 2013
585
modelist = read_mode_list(diag_mode);
Dec 17, 2013
Dec 17, 2013
586
#endif /* APP_SYNC */
Mar 1, 2012
Mar 1, 2012
587
May 16, 2011
May 16, 2011
588
589
if(check_trigger())
trigger_init();
Nov 13, 2012
Nov 13, 2012
590
Jan 21, 2013
Jan 21, 2013
591
/* Set-up mac address before kmod */
Jan 18, 2013
Jan 18, 2013
592
if(access("/etc/modprobe.d/g_ether.conf", F_OK) != 0)
Jan 17, 2013
Jan 17, 2013
593
594
595
{
generate_random_mac();
}
Jan 21, 2013
Jan 21, 2013
596
Oct 22, 2013
Oct 22, 2013
597
/* kmod init */
Aug 5, 2014
Aug 5, 2014
598
usb_moded_module_ctx_init();
Jan 21, 2013
Jan 21, 2013
599
Aug 2, 2013
Aug 2, 2013
600
601
602
/* Android specific stuff */
if(android_settings())
android_init_values();
Mar 22, 2011
Mar 22, 2011
603
604
605
606
607
608
609
610
611
612
613
/* TODO: add more start-up clean-up and init here if needed */
}
/* charging fallback handler */
static gboolean charging_fallback(gpointer data)
{
/* if a mode has been set we don't want it changed to charging
* after 5 seconds. We set it to ask, so anything different
* means a mode has been set */
if(strcmp(get_usb_mode(), MODE_ASK) != 0)
return FALSE;
Feb 1, 2013
Feb 1, 2013
614
Aug 26, 2014
Aug 26, 2014
615
set_usb_mode(MODE_CHARGING_FALLBACK);
Mar 22, 2011
Mar 22, 2011
616
617
618
/* since this is the fallback, we keep an indication
for the UI, as we are not really in charging mode.
*/
Oct 29, 2013
Oct 29, 2013
619
620
free(current_mode.mode);
current_mode.mode = strdup(MODE_ASK);
Jun 6, 2013
Jun 6, 2013
621
current_mode.data = NULL;
Jul 14, 2011
Jul 14, 2011
622
charging_timeout = 0;
Mar 22, 2011
Mar 22, 2011
623
624
625
626
627
log_info("Falling back on charging mode.\n");
return(FALSE);
}
Oct 22, 2013
Oct 22, 2013
628
629
630
static void handle_exit(void)
{
/* exiting and clean-up when mainloop ended */
Dec 17, 2013
Dec 17, 2013
631
appsync_stop();
Oct 22, 2013
Oct 22, 2013
632
633
hwal_cleanup();
usb_moded_dbus_cleanup();
Aug 5, 2014
Aug 5, 2014
634
#ifdef MEEGOLOCK
Oct 22, 2013
Oct 22, 2013
635
stop_devicelock_listener();
Aug 5, 2014
Aug 5, 2014
636
#endif /* MEEGOLOCK */
Oct 22, 2013
Oct 22, 2013
637
638
free_mode_list(modelist);
Aug 5, 2014
Aug 5, 2014
639
usb_moded_module_ctx_cleanup();
Oct 22, 2013
Oct 22, 2013
640
641
642
#ifdef APP_SYNC
free_appsync_list();
Nov 25, 2013
Nov 25, 2013
643
#ifdef APP_SYNC_DBUS
Oct 22, 2013
Oct 22, 2013
644
usb_moded_appsync_cleanup();
Nov 25, 2013
Nov 25, 2013
645
646
#endif /* APP_SYNC_DBUS */
#endif /* APP_SYNC */
Mar 11, 2014
Mar 11, 2014
647
648
/* dbus_shutdown(); This causes exit(1) and don't seem
to behave as documented */
Oct 22, 2013
Oct 22, 2013
649
650
651
652
653
654
655
/* If the mainloop is initialised, unreference it */
if (mainloop != NULL)
{
g_main_loop_quit(mainloop);
g_main_loop_unref(mainloop);
}
Nov 11, 2014
Nov 11, 2014
656
657
free(current_mode.mode);
free(current_mode.module);
Oct 22, 2013
Oct 22, 2013
658
Mar 11, 2014
Mar 11, 2014
659
660
log_debug("All resources freed. Exiting!\n");
Oct 22, 2013
Oct 22, 2013
661
662
663
664
665
exit(0);
}
static void sigint_handler(int signum)
{
Feb 18, 2014
Feb 18, 2014
666
667
struct mode_list_elem *data;
Mar 11, 2014
Mar 11, 2014
668
if(signum == SIGINT || signum == SIGTERM)
Nov 19, 2013
Nov 19, 2013
669
670
671
handle_exit();
if(signum == SIGHUP)
{
Feb 18, 2014
Feb 18, 2014
672
673
/* clean up current mode */
data = get_usb_mode_data();
Apr 30, 2014
Apr 30, 2014
674
set_disconnected_silent(data);
Feb 18, 2014
Feb 18, 2014
675
/* clear existing data to be sure */
Feb 26, 2014
Feb 26, 2014
676
set_usb_mode_data(NULL);
Nov 19, 2013
Nov 19, 2013
677
678
/* free and read in modelist again */
free_mode_list(modelist);
Mar 21, 2014
Mar 21, 2014
679
680
modelist = read_mode_list(diag_mode);
Mar 18, 2014
Mar 18, 2014
681
Mar 21, 2014
Mar 21, 2014
682
send_supported_modes_signal();
Nov 19, 2013
Nov 19, 2013
683
}
Oct 22, 2013
Oct 22, 2013
684
685
}
Mar 22, 2011
Mar 22, 2011
686
687
688
689
690
691
692
/* Display usage information */
static void usage(void)
{
fprintf(stdout,
"Usage: usb_moded [OPTION]...\n"
"USB mode daemon\n"
"\n"
Jun 2, 2014
Jun 2, 2014
693
" -a, --android_usb_broken \tkeep gadget active on broken android kernels\n"
Apr 19, 2015
Apr 19, 2015
694
" -i, --android_usb_broken_udev_events \tignore incorrect disconnect events after mode setting\n"
Jun 2, 2014
Jun 2, 2014
695
696
697
698
699
700
701
" -f, --fallback \tassume always connected\n"
" -s, --force-syslog \t\tlog to syslog\n"
" -T, --force-stderr \t\tlog to stderr\n"
" -D, --debug \t\tturn on debug printing\n"
" -d, --diag \t\tturn on diag mode\n"
" -h, --help \t\tdisplay this help and exit\n"
" -r, --rescue \t\trescue mode\n"
Jan 20, 2014
Jan 20, 2014
702
#ifdef SYSTEMD
Jun 2, 2014
Jun 2, 2014
703
" -n, --systemd \t\tnotify systemd when started up\n"
Jan 20, 2014
Jan 20, 2014
704
#endif
Jun 2, 2014
Jun 2, 2014
705
" -v, --version \t\toutput version information and exit\n"
Mar 22, 2011
Mar 22, 2011
706
707
708
"\n");
}
Dec 15, 2015
Dec 15, 2015
709
void send_supported_modes_signal(void)
Mar 18, 2014
Mar 18, 2014
710
711
712
713
714
715
716
{
/* Send supported modes signal */
gchar *mode_list = get_mode_list();
usb_moded_send_supported_modes_signal(mode_list);
g_free(mode_list);
}
Apr 4, 2016
Apr 4, 2016
717
718
719
720
721
722
723
724
725
726
void send_hidden_modes_signal(void)
{
/* Send hidden modes signal */
gchar *mode_list = get_hidden_modes();
if(mode_list) {
usb_moded_send_hidden_modes_signal(mode_list);
g_free(mode_list);
}
}
Mar 26, 2014
Mar 26, 2014
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/** Pipe fd for transferring signals to mainloop context */
static int sigpipe_fd = -1;
/** Glib io watch callback for reading signals from signal pipe
*
* @param channel glib io channel
* @param condition wakeup reason
* @param data user data (unused)
*
* @return TRUE to keep the iowatch, or FALSE to disable it
*/
static gboolean sigpipe_read_signal_cb(GIOChannel *channel,
GIOCondition condition,
gpointer data)
{
gboolean keep_watch = FALSE;
int fd, rc, sig;
(void)data;
/* Should never happen, but we must disable the io watch
* if the pipe fd still goes into unexpected state ... */
if( condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) )
goto EXIT;
if( (fd = g_io_channel_unix_get_fd(channel)) == -1 )
goto EXIT;
/* If the actual read fails, terminate with core dump */
rc = TEMP_FAILURE_RETRY(read(fd, &sig, sizeof sig));
if( rc != (int)sizeof sig )
abort();
/* handle the signal */
log_warning("handle signal: %s\n", strsignal(sig));
sigint_handler(sig);
keep_watch = TRUE;
EXIT:
if( !keep_watch )
log_crit("disabled signal handler io watch\n");
return keep_watch;
}
/** Async signal handler for writing signals to signal pipe
*
* @param sig the signal number to pass to mainloop via pipe
*/
static void sigpipe_write_signal_cb(int sig)
{
/* NOTE: This function *MUST* be kept async-signal-safe! */
static volatile int exit_tries = 0;
int rc;
/* Restore signal handler */
signal(sig, sigpipe_write_signal_cb);
switch( sig )
{
case SIGINT:
case SIGQUIT:
case SIGTERM:
/* If we receive multiple signals that should have
* caused the process to exit, assume that mainloop
* is stuck and terminate with core dump. */
if( ++exit_tries >= 2 )
abort();
break;
default:
break;
}
/* Transfer the signal to mainloop via pipe ... */
rc = TEMP_FAILURE_RETRY(write(sigpipe_fd, &sig, sizeof sig));
/* ... or terminate with core dump in case of failures */
if( rc != (int)sizeof sig )
abort();
}
/** Create a pipe and io watch for handling signal from glib mainloop
*
* @return TRUE on success, or FALSE in case of errors
*/
static gboolean sigpipe_crate_pipe(void)
{
gboolean res = FALSE;
GIOChannel *chn = 0;
int pfd[2] = { -1, -1 };
if( pipe2(pfd, O_CLOEXEC) == -1 )
goto EXIT;
if( (chn = g_io_channel_unix_new(pfd[0])) == 0 )
goto EXIT;
if( !g_io_add_watch(chn, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
sigpipe_read_signal_cb, 0) )
goto EXIT;
g_io_channel_set_close_on_unref(chn, TRUE), pfd[0] = -1;
sigpipe_fd = pfd[1], pfd[1] = -1;
res = TRUE;
EXIT:
if( chn ) g_io_channel_unref(chn);
if( pfd[0] != -1 ) close(pfd[0]);
if( pfd[1] != -1 ) close(pfd[1]);
return res;
}
/** Install async signal handlers
*/
static void sigpipe_trap_signals(void)
{
static const int sig[] =
{
SIGINT,
SIGQUIT,
SIGTERM,
SIGHUP,
-1
};
for( size_t i = 0; sig[i] != -1; ++i )
{
signal(sig[i], sigpipe_write_signal_cb);
}
}
/** Initialize signal trapping
*
* @return TRUE on success, or FALSE in case of errors
*/
static gboolean sigpipe_init(void)
{
gboolean success = FALSE;
if( !sigpipe_crate_pipe() )
goto EXIT;
sigpipe_trap_signals();
success = TRUE;
EXIT:
return success;
}
Mar 22, 2011
Mar 22, 2011
884
885
886
887
888
889
int main(int argc, char* argv[])
{
int result = EXIT_FAILURE;
int opt = 0, opt_idx = 0;
struct option const options[] = {
Jun 2, 2014
Jun 2, 2014
890
{ "android_usb_broken", no_argument, 0, 'a' },
Apr 19, 2015
Apr 19, 2015
891
{ "android_usb_broken_udev_events", no_argument, 0, 'i' },
Oct 2, 2012
Oct 2, 2012
892
{ "fallback", no_argument, 0, 'd' },
Mar 22, 2011
Mar 22, 2011
893
894
895
{ "force-syslog", no_argument, 0, 's' },
{ "force-stderr", no_argument, 0, 'T' },
{ "debug", no_argument, 0, 'D' },
Sep 10, 2013
Sep 10, 2013
896
{ "diag", no_argument, 0, 'd' },
Mar 22, 2011
Mar 22, 2011
897
{ "help", no_argument, 0, 'h' },
Nov 7, 2012
Nov 7, 2012
898
{ "rescue", no_argument, 0, 'r' },
Jan 20, 2014
Jan 20, 2014
899
{ "systemd", no_argument, 0, 'n' },
Mar 22, 2011
Mar 22, 2011
900
901
902
903
904
905
906
{ "version", no_argument, 0, 'v' },
{ 0, 0, 0, 0 }
};
log_name = basename(*argv);
/* Parse the command-line options */
Apr 19, 2015
Apr 19, 2015
907
while ((opt = getopt_long(argc, argv, "aifsTDdhrnv", options, &opt_idx)) != -1)
Mar 22, 2011
Mar 22, 2011
908
909
910
{
switch (opt)
{
Jun 2, 2014
Jun 2, 2014
911
912
913
case 'a':
android_broken_usb = TRUE;
break;
Apr 19, 2015
Apr 19, 2015
914
915
916
case 'i':
android_ignore_udev_events = TRUE;
break;
Oct 2, 2012
Oct 2, 2012
917
918
919
case 'f':
hw_fallback = TRUE;
break;
Mar 22, 2011
Mar 22, 2011
920
921
922
923
924
925
926
927
928
929
930
931
case 's':
log_type = LOG_TO_SYSLOG;
break;
case 'T':
log_type = LOG_TO_STDERR;
break;
case 'D':
log_level = LOG_DEBUG;
break;
Sep 10, 2013
Sep 10, 2013
932
933
934
935
case 'd':
diag_mode = TRUE;
break;
Mar 22, 2011
Mar 22, 2011
936
937
938
939
case 'h':
usage();
exit(0);
Nov 7, 2012
Nov 7, 2012
940
941
942
case 'r':
rescue_mode = TRUE;
break;
Jan 20, 2014
Jan 20, 2014
943
#ifdef SYSTEMD
Jan 20, 2014
Jan 20, 2014
944
945
946
case 'n':
systemd_notify = TRUE;
break;
Jan 20, 2014
Jan 20, 2014
947
#endif
Mar 22, 2011
Mar 22, 2011
948
949
950
951
952
953
954
955
956
957
case 'v':
printf("USB mode daemon version: %s\n", VERSION);
exit(0);
default:
usage();
exit(0);
}
}
Oct 16, 2014
Oct 16, 2014
958
printf("usb_moded %s starting\n", VERSION);
Mar 22, 2011
Mar 22, 2011
959
960
961
962
963
964
/* silence system() calls */
if(log_type != LOG_TO_STDERR || log_level != LOG_DEBUG )
{
freopen("/dev/null", "a", stdout);
freopen("/dev/null", "a", stderr);
}
Sep 10, 2013
Sep 10, 2013
965
#if !GLIB_CHECK_VERSION(2, 36, 0)
Mar 22, 2011
Mar 22, 2011
966
g_type_init();
Sep 10, 2013
Sep 10, 2013
967
#endif
Aug 15, 2012
Aug 15, 2012
968
#if !GLIB_CHECK_VERSION(2, 31, 0)
Apr 6, 2011
Apr 6, 2011
969
g_thread_init(NULL);
Aug 15, 2012
Aug 15, 2012
970
#endif
Mar 22, 2011
Mar 22, 2011
971
972
973
974
975
976
977
978
979
980
981
mainloop = g_main_loop_new(NULL, FALSE);
/* init daemon into a clean state first, then dbus and hw_abstraction last */
usb_moded_init();
if( !usb_moded_dbus_init() )
{
log_crit("dbus ipc init failed\n");
goto EXIT;
}
if( !hwal_init() )
{
Mar 9, 2015
Mar 9, 2015
982
983
/* if hw_fallback is active we can live with a failed hwal_init */
if(!hw_fallback)
Nov 7, 2012
Nov 7, 2012
984
{
Mar 9, 2015
Mar 9, 2015
985
log_crit("hwal init failed\n");
Oct 2, 2012
Oct 2, 2012
986
goto EXIT;
Mar 9, 2015
Mar 9, 2015
987
}
Mar 22, 2011
Mar 22, 2011
988
}
Mar 1, 2012
Mar 1, 2012
989
#ifdef MEEGOLOCK
Mar 22, 2011
Mar 22, 2011
990
start_devicelock_listener();
Mar 1, 2012
Mar 1, 2012
991
#endif /* MEEGOLOCK */
Mar 22, 2011
Mar 22, 2011
992
Oct 22, 2013
Oct 22, 2013
993
/* signal handling */
Mar 26, 2014
Mar 26, 2014
994
995
996
997
998
if( !sigpipe_init() )
{
log_crit("signal handler init failed\n");
goto EXIT;
}
Oct 22, 2013
Oct 22, 2013
999
Jan 20, 2014
Jan 20, 2014
1000
#ifdef SYSTEMD