Skip to content

Latest commit

 

History

History
397 lines (334 loc) · 10.6 KB

usb_moded-control.c

File metadata and controls

397 lines (334 loc) · 10.6 KB
 
1
2
3
/**
* @file usb_moded-control.c
*
Feb 7, 2019
Feb 7, 2019
4
* Copyright (C) 2013-2019 Jolla. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
*
* @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
*/
#include "usb_moded-control.h"
#include "usb_moded-config-private.h"
#include "usb_moded-dbus-private.h"
Sep 5, 2018
Sep 5, 2018
28
#include "usb_moded-dyn-config.h"
29
30
31
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-worker.h"
Sep 5, 2018
Sep 5, 2018
32
33
34
#include <string.h>
#include <stdlib.h>
35
36
37
38
39
40
41
42
43
44
45
46
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- usbmoded -- */
void control_rethink_usb_charging_fallback(void);
const char *control_get_external_mode (void);
static void control_set_external_mode (const char *mode);
void control_clear_external_mode (void);
static void control_update_external_mode (void);
Feb 7, 2019
Feb 7, 2019
47
48
49
const char *control_get_target_mode (void);
static void control_set_target_mode (const char *mode);
void control_clear_target_mode (void);
50
51
52
const char *control_get_usb_mode (void);
void control_clear_internal_mode (void);
void control_set_usb_mode (const char *mode);
Feb 7, 2019
Feb 7, 2019
53
void control_mode_switched (const char *mode);
54
55
56
57
58
59
60
61
62
63
64
65
void control_select_usb_mode (void);
void control_set_cable_state (cable_state_t cable_state);
cable_state_t control_get_cable_state (void);
void control_clear_cable_state (void);
bool control_get_connection_state (void);
/* ========================================================================= *
* Data
* ========================================================================= */
/* The external mode;
*
Feb 7, 2019
Feb 7, 2019
66
* What was the last current mode signaled over D-Bus.
Sep 5, 2018
Sep 5, 2018
68
static char *control_external_mode = NULL;
Feb 7, 2019
Feb 7, 2019
70
71
72
73
74
75
/* The target mode;
*
* What was the last target mode signaled over D-Bus.
*/
static char *control_target_mode = NULL;
76
77
78
79
/** The logical mode name
*
* Full set of valid modes can occur here
*/
Sep 5, 2018
Sep 5, 2018
80
static char *control_internal_mode = NULL;
81
82
83
84
85
86
87
/** Connection status
*
* Access only via:
* - control_set_cable_state()
* - control_get_connection_state()
*/
Sep 5, 2018
Sep 5, 2018
88
static cable_state_t control_cable_state = CABLE_STATE_UNKNOWN;
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
144
145
146
147
/* ========================================================================= *
* Functions
* ========================================================================= */
/** Check if we can/should leave charging fallback mode
*
* Called when device lock status, or device status (dsme)
* changes.
*/
void
control_rethink_usb_charging_fallback(void)
{
/* Cable must be connected to a pc */
if( control_get_cable_state() != CABLE_STATE_PC_CONNECTED )
goto EXIT;
/* Switching can happen only from MODE_UNDEFINED
* or MODE_CHARGING_FALLBACK */
const char *usb_mode = control_get_usb_mode();
if( strcmp(usb_mode, MODE_UNDEFINED) &&
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;
if( !usbmoded_can_export() ) {
log_notice("exporting data not allowed; stay in %s", usb_mode);
goto EXIT;
}
log_debug("attempt to leave %s", usb_mode);
control_select_usb_mode();
EXIT:
return;
}
const char *control_get_external_mode(void)
{
return control_external_mode ?: MODE_UNDEFINED;
}
static void control_set_external_mode(const char *mode)
{
gchar *previous = control_external_mode;
if( !g_strcmp0(previous, mode) )
goto EXIT;
log_debug("external_mode: %s -> %s",
previous, mode);
control_external_mode = g_strdup(mode);
g_free(previous);
// DO THE DBUS BROADCAST
if( !strcmp(control_external_mode, MODE_ASK) ) {
/* send signal, mode will be set when the dialog service calls
* the set_mode method call. */
Feb 7, 2019
Feb 7, 2019
148
umdbus_send_event_signal(USB_CONNECTED_DIALOG_SHOW);
Feb 7, 2019
Feb 7, 2019
151
152
153
154
155
156
157
158
159
160
161
162
umdbus_send_current_state_signal(control_external_mode);
if( strcmp(control_external_mode, MODE_BUSY) ) {
/* Stable state reached. Synchronize target state.
*
* Note that normally this ends up being a nop,
* but might be needed if the originally scheduled
* target could not be reached due to errors / user
* disconnecting the cable.
*/
control_set_target_mode(control_external_mode);
}
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
EXIT:
return;
}
void control_clear_external_mode(void)
{
g_free(control_external_mode),
control_external_mode = 0;
}
static void control_update_external_mode(void)
{
const char *internal_mode = control_get_usb_mode();
const char *external_mode = common_map_mode_to_external(internal_mode);
control_set_external_mode(external_mode);
}
Feb 7, 2019
Feb 7, 2019
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
const char *control_get_target_mode(void)
{
return control_target_mode ?: MODE_UNDEFINED;
}
static void control_set_target_mode(const char *mode)
{
gchar *previous = control_target_mode;
if( !g_strcmp0(previous, mode) )
goto EXIT;
log_debug("target_mode: %s -> %s",
previous, mode);
control_target_mode = g_strdup(mode);
g_free(previous);
umdbus_send_target_state_signal(control_target_mode);
EXIT:
return;
}
void control_clear_target_mode(void)
{
g_free(control_target_mode),
control_target_mode = 0;
}
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/** get the usb mode
*
* @return the currently set mode
*
*/
const char * control_get_usb_mode(void)
{
return control_internal_mode;
}
void control_clear_internal_mode(void)
{
g_free(control_internal_mode),
control_internal_mode = 0;
}
/** set the usb mode
*
* @param mode The requested USB mode
*/
void control_set_usb_mode(const char *mode)
{
gchar *previous = control_internal_mode;
if( !g_strcmp0(previous, mode) )
goto EXIT;
log_debug("internal_mode: %s -> %s",
previous, mode);
control_internal_mode = g_strdup(mode);
g_free(previous);
Feb 7, 2019
Feb 7, 2019
243
244
245
/* Update target mode before declaring busy */
control_set_target_mode(control_internal_mode);
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
/* Invalidate current mode for the duration of mode transition */
control_set_external_mode(MODE_BUSY);
/* Propagate down to gadget config */
worker_request_hardware_mode(control_internal_mode);
EXIT:
return;
}
/* Worker thread has finished mode switch
*
* @param mode The activated USB mode
*/
void control_mode_switched(const char *mode)
{
/* Update state data - without retriggering the worker thread
*/
if( g_strcmp0(control_internal_mode, mode) ) {
log_debug("internal_mode: %s -> %s",
control_internal_mode, mode);
g_free(control_internal_mode),
control_internal_mode = g_strdup(mode);
}
/* Propagate up to D-Bus */
control_update_external_mode();
return;
}
/** set the chosen usb state
*
* gauge what mode to enter and then call control_set_usb_mode()
*
*/
void control_select_usb_mode(void)
{
char *mode_to_set = 0;
Sep 5, 2018
Sep 5, 2018
286
if( usbmoded_get_rescue_mode() ) {
287
288
289
290
291
log_debug("Entering rescue mode!\n");
control_set_usb_mode(MODE_DEVELOPER);
goto EXIT;
}
Sep 5, 2018
Sep 5, 2018
292
if( usbmoded_get_diag_mode() ) {
Sep 5, 2018
Sep 5, 2018
293
294
295
296
297
298
299
/* Assumption is that in diag-mode there is only
* one mode configured i.e. list head is diag-mode. */
GList *iter = usbmoded_get_modelist();
if( !iter ) {
log_err("Diagnostic mode is not configured!");
}
else {
Sep 5, 2018
Sep 5, 2018
300
mode_list_elem_t *data = iter->data;
Sep 5, 2018
Sep 5, 2018
301
log_debug("Entering diagnostic mode!");
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
control_set_usb_mode(data->mode_name);
}
goto EXIT;
}
mode_to_set = config_get_mode_setting();
/* If there is only one allowed mode, use it without
* going through ask-mode */
if( !strcmp(MODE_ASK, mode_to_set) ) {
// FIXME free() vs g_free() conflict
gchar *available = common_get_mode_list(AVAILABLE_MODES_LIST);
if( *available && !strchr(available, ',') ) {
free(mode_to_set), mode_to_set = available, available = 0;
}
g_free(available);
}
if( mode_to_set && usbmoded_can_export() ) {
control_set_usb_mode(mode_to_set);
}
else {
/* config is corrupted or we do not have a mode configured, fallback to charging
* 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.
*/
control_set_usb_mode(MODE_CHARGING_FALLBACK);
}
EXIT:
free(mode_to_set);
}
/** set the usb connection status
*
* @param cable_state CABLE_STATE_DISCONNECTED, ...
*/
void control_set_cable_state(cable_state_t cable_state)
{
cable_state_t prev = control_cable_state;
control_cable_state = cable_state;
if( control_cable_state == prev )
goto EXIT;
log_debug("control_cable_state: %s -> %s",
cable_state_repr(prev),
cable_state_repr(control_cable_state));
switch( control_cable_state ) {
default:
case CABLE_STATE_DISCONNECTED:
control_set_usb_mode(MODE_UNDEFINED);
break;
case CABLE_STATE_CHARGER_CONNECTED:
control_set_usb_mode(MODE_CHARGER);
break;
case CABLE_STATE_PC_CONNECTED:
control_select_usb_mode();
break;
}
EXIT:
return;
}
/** get the usb connection status
*
* @return CABLE_STATE_DISCONNECTED, ...
*/
cable_state_t control_get_cable_state(void)
{
return control_cable_state;
}
void control_clear_cable_state(void)
{
control_cable_state = CABLE_STATE_UNKNOWN;
}
/** Get if the cable (pc or charger) is connected or not
*
* @ return true if connected, false if disconnected
*/
bool control_get_connection_state(void)
{
bool connected = false;
switch( control_get_cable_state() ) {
case CABLE_STATE_CHARGER_CONNECTED:
case CABLE_STATE_PC_CONNECTED:
connected = true;
break;
default:
break;
}
return connected;
}