diff --git a/configure.ac b/configure.ac index 8914e61..ab783de 100644 --- a/configure.ac +++ b/configure.ac @@ -22,7 +22,7 @@ test_gcc_flag() { # We use gnu99 instead of c99 because many have interpreted the standard # in a way that int64_t isn't defined on non-64 bit platforms. -CFLAGS="-Os -std=gnu99 -Wall -W -Wextra -pipe -Wformat -Wold-style-definition -Wdeclaration-after-statement -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wno-unused-parameter -finline-small-functions -Wno-unused-result -fstack-protector -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now -fPIE -fpie -pie" +CFLAGS="-Os -std=gnu99 -Wall -Wextra -pipe -Wold-style-definition -Wfloat-equal -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wendif-labels -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wno-unused-parameter -finline-small-functions -Wno-unused-result -fstack-protector -D_FORTIFY_SOURCE=2 -Wl,-z,relro,-z,now -fPIE -fpie -pie" LDFLAGS="-z relro -z now" AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug],[Enable debug @<:@default=false@:>@]), diff --git a/src/usb_moded-devicelock.c b/src/usb_moded-devicelock.c index d9ad5bd..23e1986 100644 --- a/src/usb_moded-devicelock.c +++ b/src/usb_moded-devicelock.c @@ -98,16 +98,14 @@ static gboolean devicelock_is_available = FALSE; /** Checks if the device is locked. * - * @return 0 for unlocked, 1 for locked + * @return TRUE for unlocked, FALSE for locked * */ -int usb_moded_get_export_permission(void) +gboolean usb_moded_get_export_permission(void) { gboolean unlocked = (device_lock_state == DEVICE_LOCK_UNLOCKED); - // FIXME: this is reverse from what function name makes to expect - - return unlocked ? 0 : 1; + return unlocked; } /* ========================================================================= * @@ -116,29 +114,18 @@ int 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; diff --git a/src/usb_moded-dsme.c b/src/usb_moded-dsme.c index 0b04414..1896781 100644 --- a/src/usb_moded-dsme.c +++ b/src/usb_moded-dsme.c @@ -26,6 +26,8 @@ #include #include "usb_moded-dsme.h" +#include "usb_moded.h" + #include "usb_moded-dbus-private.h" #include "usb_moded-log.h" @@ -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; diff --git a/src/usb_moded-lock.h b/src/usb_moded-lock.h index dd91fcd..8e5803d 100644 --- a/src/usb_moded-lock.h +++ b/src/usb_moded-lock.h @@ -26,6 +26,6 @@ */ /*============================================================================= */ -int usb_moded_get_export_permission(void); +gboolean usb_moded_get_export_permission(void); gboolean start_devicelock_listener(void); void stop_devicelock_listener(void); diff --git a/src/usb_moded-trigger.c b/src/usb_moded-trigger.c index f7e9114..e09f346 100644 --- a/src/usb_moded-trigger.c +++ b/src/usb_moded-trigger.c @@ -211,7 +211,7 @@ static void udev_parse(struct udev_device *dev) if(!strcmp(tmp, trigger)) { #if defined MEEGOLOCK - if(!usb_moded_get_export_permission()) + if(usb_moded_get_export_permission()) #endif /* MEEGOLOCK */ if(strcmp(get_trigger_mode(), get_usb_mode()) != 0) { @@ -230,7 +230,7 @@ static void udev_parse(struct udev_device *dev) /* for triggers without trigger value */ { #if defined MEEGOLOCK - if(!usb_moded_get_export_permission()) + if(usb_moded_get_export_permission()) #endif /* MEEGOLOCK */ if(strcmp(get_trigger_mode(), get_usb_mode()) != 0) { diff --git a/src/usb_moded.c b/src/usb_moded.c index 9962920..0a1fbcd 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -59,6 +59,11 @@ #include "usb_moded-dsme.h" #endif +/* Wakelogging is noisy, do not log it by default */ +#ifndef VERBOSE_WAKELOCKING +# define VERBOSE_WAKELOCKING 0 +#endif + /* global definitions */ static int usb_moded_exitcode = EXIT_FAILURE; @@ -246,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 * @@ -253,9 +298,6 @@ void set_charger_connected(gboolean state) void set_usb_connected_state(void) { char *mode_to_set; -#ifdef MEEGOLOCK - int export = 1; /* assume locked */ -#endif /* MEEGOLOCK */ if(rescue_mode) { @@ -279,10 +321,10 @@ void set_usb_connected_state(void) #ifdef MEEGOLOCK /* check if we are allowed to export system contents 0 is unlocked */ - export = usb_moded_get_export_permission(); + gboolean can_export = usb_moded_get_export_permission(); /* 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()) + if(mode_to_set && can_export && is_in_user_state()) #else if(mode_to_set) #endif /* MEEGOLOCK */ @@ -332,11 +374,6 @@ void set_usb_mode(const char *mode) /* set return to 1 to be sure to error out if no matching mode is found either */ int ret=1, net=0; -#ifdef MEEGOLOCK - /* Do a second check in case timer suspend causes a race issue */ - int export = 1; -#endif - log_debug("Setting %s\n", mode); /* CHARGING AND FALLBACK CHARGING are always ok to set, so this can be done @@ -372,9 +409,9 @@ void set_usb_mode(const char *mode) /* In ACTDEAD export is always ok */ if(is_in_user_state()) { - export = usb_moded_get_export_permission(); + gboolean can_export = usb_moded_get_export_permission(); - if(export && !rescue_mode) + if(!can_export && !rescue_mode) { log_debug("Secondary device lock check failed. Not setting mode!\n"); goto end; @@ -978,7 +1015,9 @@ void acquire_wakelock(const char *wakelock_name) USB_MODED_SUSPEND_DELAY_MAXIMUM_MS * 1000000LL); write_to_sysfs_file("/sys/power/wake_lock", buff); +#if VERBOSE_WAKELOCKING log_debug("acquire_wakelock %s", wakelock_name); +#endif } /** Release wakelock via sysfs @@ -987,7 +1026,9 @@ void acquire_wakelock(const char *wakelock_name) */ void release_wakelock(const char *wakelock_name) { +#if VERBOSE_WAKELOCKING log_debug("release_wakelock %s", wakelock_name); +#endif write_to_sysfs_file("/sys/power/wake_unlock", wakelock_name); } diff --git a/src/usb_moded.h b/src/usb_moded.h index ed7be40..2abc46a 100644 --- a/src/usb_moded.h +++ b/src/usb_moded.h @@ -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);