Skip to content

Latest commit

 

History

History
515 lines (437 loc) · 14.4 KB

usb_moded-common.c

File metadata and controls

515 lines (437 loc) · 14.4 KB
 
1
2
3
4
5
#include "usb_moded-common.h"
#include "usb_moded.h"
#include "usb_moded-config-private.h"
#include "usb_moded-dbus-private.h"
Sep 5, 2018
Sep 5, 2018
6
#include "usb_moded-dyn-config.h"
7
8
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
Sep 5, 2018
Sep 5, 2018
9
10
11
12
13
14
15
#include "usb_moded-worker.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
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
/* ========================================================================= *
* Types
* ========================================================================= */
/** Mapping usb mode from internal to hardware/broadcast use */
typedef struct modemapping_t
{
/** Any valid usb mode */
const char *internal_mode;
/** Mode to use for usb configuration, or NULL = internal */
const char *hardware_mode;
/** Mode to use for D-Bus broadcast, or NULL = internal */
const char *external_mode;
} modemapping_t;
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- cable -- */
const char *cable_state_repr(cable_state_t state);
/* -- common -- */
const char *common_map_mode_to_hardware (const char *internal_mode);
const char *common_map_mode_to_external (const char *internal_mode);
void common_send_supported_modes_signal (void);
void common_send_available_modes_signal (void);
void common_send_hidden_modes_signal (void);
void common_send_whitelisted_modes_signal(void);
static void common_write_to_sysfs_file (const char *path, const char *text);
void common_acquire_wakelock (const char *wakelock_name);
void common_release_wakelock (const char *wakelock_name);
int common_system_ (const char *file, int line, const char *func, const char *command);
FILE *common_popen_ (const char *file, int line, const char *func, const char *command, const char *type);
Sep 5, 2018
Sep 5, 2018
55
56
waitres_t common_wait (unsigned tot_ms, bool (*ready_cb)(void *aptr), void *aptr);
bool common_msleep_ (const char *file, int line, const char *func, unsigned msec);
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
static bool common_mode_in_list (const char *mode, char *const *modes);
int common_valid_mode (const char *mode);
gchar *common_get_mode_list (mode_list_type_t type);
/* ========================================================================= *
* Functions
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* CABLE_STATE
* ------------------------------------------------------------------------- */
const char *cable_state_repr(cable_state_t state)
{
static const char * const lut[CABLE_STATE_NUMOF] = {
[CABLE_STATE_UNKNOWN] = "unknown",
[CABLE_STATE_DISCONNECTED] = "disconnected",
[CABLE_STATE_CHARGER_CONNECTED] = "charger_connected",
[CABLE_STATE_PC_CONNECTED] = "pc_connected",
};
return lut[state];
}
/* ------------------------------------------------------------------------- *
* MODE_MAPPING
* ------------------------------------------------------------------------- */
static const modemapping_t common_modemapping[] =
{
{
.internal_mode = MODE_UNDEFINED,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_ASK,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_MASS_STORAGE,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_DEVELOPER,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_MTP,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_HOST,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_CONNECTION_SHARING,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_DIAG,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_ADB,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_PC_SUITE,
.hardware_mode = 0,
.external_mode = 0,
},
{
.internal_mode = MODE_CHARGING,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
{
.internal_mode = MODE_CHARGING_FALLBACK,
.hardware_mode = MODE_CHARGING,
Sep 5, 2018
Sep 5, 2018
144
.external_mode = 0,
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
},
{
.internal_mode = MODE_CHARGER,
.hardware_mode = MODE_CHARGING,
.external_mode = 0,
},
// sentinel
{
.internal_mode = 0,
.hardware_mode = 0,
.external_mode = 0,
}
};
const char *
common_map_mode_to_hardware(const char *internal_mode)
{
const char *hardware_mode = 0;
for( size_t i = 0; common_modemapping[i].internal_mode; ++i ) {
if( strcmp(common_modemapping[i].internal_mode, internal_mode) )
continue;
hardware_mode = common_modemapping[i].hardware_mode;
break;
}
return hardware_mode ?: internal_mode;
}
const char *
common_map_mode_to_external(const char *internal_mode)
{
const char *external_mode = 0;
for( size_t i = 0; common_modemapping[i].internal_mode; ++i ) {
if( strcmp(common_modemapping[i].internal_mode, internal_mode) )
continue;
external_mode = common_modemapping[i].external_mode;
break;
}
return external_mode ?: internal_mode;
}
/* ------------------------------------------------------------------------- *
* DBUS_NOTIFICATIONS
* ------------------------------------------------------------------------- */
/** Send supported modes signal
*/
void common_send_supported_modes_signal(void)
{
gchar *mode_list = common_get_mode_list(SUPPORTED_MODES_LIST);
umdbus_send_supported_modes_signal(mode_list);
g_free(mode_list);
}
/** Send available modes signal
*/
void common_send_available_modes_signal(void)
{
gchar *mode_list = common_get_mode_list(AVAILABLE_MODES_LIST);
umdbus_send_available_modes_signal(mode_list);
g_free(mode_list);
}
/** Send hidden modes signal
*/
void common_send_hidden_modes_signal(void)
{
gchar *mode_list = config_get_hidden_modes();
Sep 5, 2018
Sep 5, 2018
214
215
umdbus_send_hidden_modes_signal(mode_list);
g_free(mode_list);
216
217
218
219
220
221
222
}
/** Send whitelisted modes signal
*/
void common_send_whitelisted_modes_signal(void)
{
gchar *mode_list = config_get_mode_whitelist();
Sep 5, 2018
Sep 5, 2018
223
224
umdbus_send_whitelisted_modes_signal(mode_list);
g_free(mode_list);
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
}
/* ------------------------------------------------------------------------- *
* SYSFS_IO
* ------------------------------------------------------------------------- */
/** Write string to already existing sysfs file
*
* Note: Attempts to write to nonexisting files are silently ignored.
*
* @param path Where to write
* @param text What to write
*/
static void common_write_to_sysfs_file(const char *path, const char *text)
{
int fd = -1;
if (!path || !text)
goto EXIT;
if ((fd = open(path, O_WRONLY)) == -1) {
if (errno != ENOENT) {
log_warning("%s: open for writing failed: %m", path);
}
goto EXIT;
}
if (write(fd, text, strlen(text)) == -1) {
log_warning("%s: write failed : %m", path);
goto EXIT;
}
EXIT:
if (fd != -1)
close(fd);
}
/* ------------------------------------------------------------------------- *
* WAKELOCKS
* ------------------------------------------------------------------------- */
/** Acquire wakelock via sysfs
*
* Wakelock must be released via common_release_wakelock().
*
* Automatically terminating wakelock is used, so that we
* do not block suspend indefinately in case usb_moded
* gets stuck or crashes.
*
* Note: The name should be unique within the system.
*
* @param wakelock_name Wake lock to be acquired
*/
void common_acquire_wakelock(const char *wakelock_name)
{
char buff[256];
snprintf(buff, sizeof buff, "%s %lld",
wakelock_name,
USB_MODED_SUSPEND_DELAY_MAXIMUM_MS * 1000000LL);
common_write_to_sysfs_file("/sys/power/wake_lock", buff);
#if VERBOSE_WAKELOCKING
log_debug("common_acquire_wakelock %s", wakelock_name);
#endif
}
/** Release wakelock via sysfs
*
* @param wakelock_name Wake lock to be released
*/
void common_release_wakelock(const char *wakelock_name)
{
#if VERBOSE_WAKELOCKING
log_debug("common_release_wakelock %s", wakelock_name);
#endif
common_write_to_sysfs_file("/sys/power/wake_unlock", wakelock_name);
}
/* ------------------------------------------------------------------------- *
* BLOCKING_OPERATION
* ------------------------------------------------------------------------- */
/** Wrapper to give visibility to blocking system() calls usb-moded is making
*/
int
common_system_(const char *file, int line, const char *func,
const char *command)
{
log_debug("EXEC %s; from %s:%d: %s()",
command, file, line, func);
int rc = system(command);
if( rc != 0 )
log_warning("EXEC %s; exit code is %d", command, rc);
return rc;
}
/** Wrapper to give visibility subprocesses usb-moded is invoking via popen()
*/
FILE *
common_popen_(const char *file, int line, const char *func,
const char *command, const char *type)
{
log_debug("EXEC %s; from %s:%d: %s()",
command, file, line, func);
return popen(command, type);
}
Sep 5, 2018
Sep 5, 2018
336
337
waitres_t
common_wait(unsigned tot_ms, bool (*ready_cb)(void *aptr), void *aptr)
Sep 5, 2018
Sep 5, 2018
339
struct timespec ts;
Sep 5, 2018
Sep 5, 2018
341
waitres_t res = WAIT_FAILED;
Sep 5, 2018
Sep 5, 2018
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
for( ;; ) {
unsigned nap_ms = (tot_ms > 200) ? 200 : tot_ms;
ts.tv_sec = (nap_ms / 1000);
ts.tv_nsec = (nap_ms % 1000);
ts.tv_nsec *= 1000 * 1000;
for( ;; ) {
if( ready_cb && ready_cb(aptr) ) {
res = WAIT_READY;
goto EXIT;
}
if( tot_ms <= 0 ) {
res = WAIT_TIMEOUT;
goto EXIT;
}
if( worker_bailing_out() ) {
log_warning("wait canceled");
goto EXIT;
}
Sep 5, 2018
Sep 5, 2018
366
367
368
369
370
371
372
if( nanosleep(&ts, &ts) == 0 )
break;
if( errno != EINTR ) {
log_warning("wait failed: %m");
goto EXIT;
}
Sep 5, 2018
Sep 5, 2018
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
tot_ms -= nap_ms;
}
EXIT:
return res;
}
/** Wrapper to give visibility to blocking sleeps usb-moded is making
*/
bool
common_msleep_(const char *file, int line, const char *func, unsigned msec)
{
log_debug("SLEEP %u.%03u seconds; from %s:%d: %s()",
msec / 1000u, msec % 1000u,file, line, func);
return common_wait(msec, 0, 0) == WAIT_TIMEOUT;
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
}
/* ------------------------------------------------------------------------- *
* MISC
* ------------------------------------------------------------------------- */
/* check if a mode is in a list */
static bool common_mode_in_list(const char *mode, char * const *modes)
{
int i;
if (!modes)
return false;
for(i = 0; modes[i] != NULL; i++)
{
if(!strcmp(modes[i], mode))
return true;
}
return false;
}
/** check if a given usb_mode exists
*
* @param mode The mode to look for
*
Sep 5, 2018
Sep 5, 2018
416
* @return 0 if mode exists, 1 if it does not exist
417
418
419
420
421
422
*/
int common_valid_mode(const char *mode)
{
int valid = 1;
/* 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 */
Sep 5, 2018
Sep 5, 2018
423
if(!strcmp(MODE_CHARGING, mode)) {
Sep 5, 2018
Sep 5, 2018
425
}
Sep 5, 2018
Sep 5, 2018
428
429
gchar *whitelist_value = 0;
gchar **whitelist_array = 0;
Sep 5, 2018
Sep 5, 2018
431
432
433
434
if( (whitelist_value = config_get_mode_whitelist()) )
whitelist_array = g_strsplit(whitelist_value, ",", 0);
for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) {
Sep 5, 2018
Sep 5, 2018
435
mode_list_elem_t *data = iter->data;
Sep 5, 2018
Sep 5, 2018
436
437
438
439
440
441
if( strcmp(mode, data->mode_name) )
continue;
if (!whitelist_array || common_mode_in_list(data->mode_name, whitelist_array))
valid = 0;
break;
Sep 5, 2018
Sep 5, 2018
443
444
445
g_strfreev(whitelist_array);
g_free(whitelist_value);
446
447
448
449
450
451
452
453
}
return valid;
}
/** make a list of all available usb modes
*
* @param type The type of list to return. Supported or available.
*
Sep 5, 2018
Sep 5, 2018
454
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
455
456
457
*/
gchar *common_get_mode_list(mode_list_type_t type)
{
Sep 5, 2018
Sep 5, 2018
458
GString *mode_list_str = g_string_new(NULL);
Sep 5, 2018
Sep 5, 2018
460
461
gchar *hidden_modes_value = 0;
gchar **hidden_modes_array = 0;
Sep 5, 2018
Sep 5, 2018
463
464
465
466
gchar *whitelist_value = 0;
gchar **whitelist_array = 0;
if( usbmoded_get_diag_mode() )
Sep 5, 2018
Sep 5, 2018
468
469
470
471
472
473
474
/* diag mode. there is only one active mode */
g_string_append(mode_list_str, MODE_DIAG);
goto EXIT;
}
if( (hidden_modes_value = config_get_hidden_modes()) )
hidden_modes_array = g_strsplit(hidden_modes_value, ",", 0);
Sep 5, 2018
Sep 5, 2018
476
477
478
479
480
481
482
483
484
485
switch( type ) {
case SUPPORTED_MODES_LIST:
/* All modes that are not hidden */
break;
case AVAILABLE_MODES_LIST:
/* All whitelisted modes that are not hidden */
if( (whitelist_value = config_get_mode_whitelist()) )
whitelist_array = g_strsplit(whitelist_value, ",", 0);
break;
Sep 5, 2018
Sep 5, 2018
487
488
for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) )
Sep 5, 2018
Sep 5, 2018
490
mode_list_elem_t *data = iter->data;
Sep 5, 2018
Sep 5, 2018
491
492
493
494
495
496
497
498
499
500
501
/* skip items in the hidden list */
if (common_mode_in_list(data->mode_name, hidden_modes_array))
continue;
/* if there is a whitelist skip items not in the list */
if (whitelist_array && !common_mode_in_list(data->mode_name, whitelist_array))
continue;
g_string_append(mode_list_str, data->mode_name);
g_string_append(mode_list_str, ", ");
Sep 5, 2018
Sep 5, 2018
503
504
505
506
507
508
509
510
511
512
513
514
/* End with charging mode */
g_string_append(mode_list_str, MODE_CHARGING);
EXIT:
g_strfreev(whitelist_array);
g_free(whitelist_value);
g_strfreev(hidden_modes_array);
g_free(hidden_modes_value);
return g_string_free(mode_list_str, false);