Skip to content

Commit

Permalink
[usb-moded] Use single function to exit charging fallback. Fixes JB#3…
Browse files Browse the repository at this point in the history
…7759

Whether and when we can switch for example from charging fallback to
developer mode depends on multiple triggers - the order of which might vary
from one device/bootup to another. Using custom state evaluation code at
each possible trigger location easily causes unexpected results like device
staying in fallback charging mode until the cable is re-attached.

Add a state evaluation function and call it from those triggers that
potentially allow exit from the charging fallback mode.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Feb 14, 2017
1 parent ad9208f commit 8286dbf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
35 changes: 12 additions & 23 deletions src/usb_moded-devicelock.c
Expand Up @@ -114,29 +114,18 @@ gboolean usb_moded_get_export_permission(void)

static void devicelock_state_changed(devicelock_state_t state)
{
if( device_lock_state != state ) {
log_debug("devicelock state: %s -> %s",
devicelock_state_repr(device_lock_state),
devicelock_state_repr(state));
device_lock_state = state;

if( device_lock_state == DEVICE_LOCK_UNLOCKED &&
get_usb_connection_state() == 1 )
{
const char *usb_mode = get_usb_mode();
log_debug("usb_mode %s\n", usb_mode);

/* if the mode is MODE_CHARGING_FALLBACK we know the user
* has not selected any mode, in case it things are still
* undefined it cannot hurt to try again to set a mode */
if(!strcmp(usb_mode, MODE_UNDEFINED) ||
!strcmp(usb_mode, MODE_CHARGING_FALLBACK))
{
log_debug("set_usb");
set_usb_connected_state();
}
}
}
if( device_lock_state == state )
goto EXIT;

log_debug("devicelock state: %s -> %s",
devicelock_state_repr(device_lock_state),
devicelock_state_repr(state));
device_lock_state = state;

rethink_usb_charging_fallback();

EXIT:
return;
}

static DBusPendingCall *devicelock_state_query_pc = 0;
Expand Down
17 changes: 12 additions & 5 deletions src/usb_moded-dsme.c
Expand Up @@ -26,6 +26,8 @@
#include <dbus/dbus-glib-lowlevel.h>

#include "usb_moded-dsme.h"
#include "usb_moded.h"

#include "usb_moded-dbus-private.h"
#include "usb_moded-log.h"

Expand Down Expand Up @@ -87,11 +89,16 @@ static void device_state_changed(const char *state)

log_debug("device state: %s", state ?: "(null)");

if( in_user_state != to_user_state ) {
in_user_state = to_user_state;
log_debug("in user state: %s",
in_user_state ? "true" : "false");
}
if( in_user_state == to_user_state )
goto EXIT;

in_user_state = to_user_state;
log_debug("in user state: %s", in_user_state ? "true" : "false");

rethink_usb_charging_fallback();

EXIT:
return;
}

static DBusPendingCall *device_state_query_pc = 0;
Expand Down
40 changes: 40 additions & 0 deletions src/usb_moded.c
Expand Up @@ -251,6 +251,46 @@ void set_charger_connected(gboolean state)
current_mode.connected = FALSE;
}
}
/** Check if we can/should leave charging fallback mode
*/
void
rethink_usb_charging_fallback(void)
{
/* Cable must be connected and suitable usb-mode mode
* selected for any of this to apply.
*/
if( !get_usb_connection_state() )
goto EXIT;

const char *usb_mode = get_usb_mode();

if( strcmp(usb_mode, MODE_UNDEFINED) &&
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;

/* If device locking is supported, the device must be in
* unlocked state (or rescue mode must be active).
*/
#ifdef MEEGOLOCK
if( !usb_moded_get_export_permission() && !rescue_mode ) {
log_notice("device is locked; stay in %s", usb_mode);
goto EXIT;
}
#endif

/* Device must be in USER state or in rescue mode
*/
if( !is_in_user_state() && !rescue_mode ) {
log_notice("device is not in USER mode; stay in %s", usb_mode);
goto EXIT;
}

log_debug("attempt to leave %s", usb_mode);
set_usb_connected_state();

EXIT:
return;
}

/** set the chosen usb state
*
Expand Down
1 change: 1 addition & 0 deletions src/usb_moded.h
Expand Up @@ -61,6 +61,7 @@ typedef struct usb_mode
void set_usb_connected(gboolean connected);
void set_usb_connected_state(void);
void set_usb_mode(const char *mode);
void rethink_usb_charging_fallback(void);
const char * get_usb_mode(void);
void set_usb_module(const char *module);
const char * get_usb_module(void);
Expand Down

0 comments on commit 8286dbf

Please sign in to comment.