Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb37759_boot_to_developer_mode' into 'master'
Boot to developer mode

See merge request !23
  • Loading branch information
spiiroin committed Feb 14, 2017
2 parents 3cd49b7 + 8286dbf commit 2b4b81e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 49 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -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@:>@]),
Expand Down
43 changes: 15 additions & 28 deletions src/usb_moded-devicelock.c
Expand Up @@ -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;
}

/* ========================================================================= *
Expand All @@ -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;
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
2 changes: 1 addition & 1 deletion src/usb_moded-lock.h
Expand Up @@ -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);
4 changes: 2 additions & 2 deletions src/usb_moded-trigger.c
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
65 changes: 53 additions & 12 deletions src/usb_moded.c
Expand Up @@ -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;
Expand Down Expand Up @@ -246,16 +251,53 @@ 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
*
*/
void set_usb_connected_state(void)
{
char *mode_to_set;
#ifdef MEEGOLOCK
int export = 1; /* assume locked */
#endif /* MEEGOLOCK */

if(rescue_mode)
{
Expand All @@ -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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
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 2b4b81e

Please sign in to comment.