Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb50877' into 'master'
[usb-moded] Set mode charging_fallback on user change. Fixes JB#50877

See merge request mer-core/usb-moded!73
  • Loading branch information
MikeSalmela committed Feb 9, 2021
2 parents 42aa42b + 5349e5b commit f366b46
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 16 deletions.
18 changes: 18 additions & 0 deletions .depend
Expand Up @@ -488,6 +488,22 @@ src/usb_moded-worker.pic.o:\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded-user.o:\
src/usb_moded-user.c\
src/usb_moded-user.h\
config-static.h\
src/usb_moded-log.h\
src/usb_moded-modes.h\
src/usb_moded-control.h\

src/usb_moded-user.pic.o:\
src/usb_moded-user.c\
src/usb_moded-user.h\
config-static.h\
src/usb_moded-log.h\
src/usb_moded-modes.h\
src/usb_moded-control.h\

src/usb_moded.o:\
src/usb_moded.c\
config-static.h\
Expand All @@ -514,6 +530,7 @@ src/usb_moded.o:\
src/usb_moded-trigger.h\
src/usb_moded-udev.h\
src/usb_moded-worker.h\
src/usb_moded-user.h\
src/usb_moded.h\

src/usb_moded.pic.o:\
Expand Down Expand Up @@ -542,6 +559,7 @@ src/usb_moded.pic.o:\
src/usb_moded-trigger.h\
src/usb_moded-udev.h\
src/usb_moded-worker.h\
src/usb_moded-user.h\
src/usb_moded.h\

utils/udev-search.o:\
Expand Down
3 changes: 3 additions & 0 deletions Makefile.custom
Expand Up @@ -204,6 +204,7 @@ usb_moded-OBJS += src/usb_moded-systemd.o
usb_moded-OBJS += src/usb_moded-trigger.o
usb_moded-OBJS += src/usb_moded-udev.o
usb_moded-OBJS += src/usb_moded-worker.o
usb_moded-OBJS += src/usb_moded-user.o

usb_moded-OBJS += dbus-gmain/dbus-gmain.o

Expand Down Expand Up @@ -273,6 +274,7 @@ CLEAN_SOURCES += src/usb_moded-trigger.c
CLEAN_SOURCES += src/usb_moded-udev.c
CLEAN_SOURCES += src/usb_moded-util.c
CLEAN_SOURCES += src/usb_moded-worker.c
CLEAN_SOURCES += src/usb_moded-user.c
CLEAN_SOURCES += src/usb_moded.c
CLEAN_SOURCES += utils/udev-search.c

Expand Down Expand Up @@ -302,6 +304,7 @@ CLEAN_HEADERS += src/usb_moded-systemd.h
CLEAN_HEADERS += src/usb_moded-trigger.h
CLEAN_HEADERS += src/usb_moded-udev.h
CLEAN_HEADERS += src/usb_moded-worker.h
CLEAN_HEADERS += src/usb_moded-user.h
CLEAN_HEADERS += src/usb_moded.h

# Files with whitespace issues
Expand Down
6 changes: 3 additions & 3 deletions config-static.h
Expand Up @@ -11,7 +11,7 @@
#define PACKAGE_NAME "usb_moded"

/* Define to the full name and version of this package. */
#define PACKAGE_STRING "usb_moded 0.86.0+mer51"
#define PACKAGE_STRING "usb_moded 0.86.0+mer52"

/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "usb_moded"
Expand All @@ -20,13 +20,13 @@
#define PACKAGE_URL ""

/* Define to the version of this package. */
#define PACKAGE_VERSION "0.86.0+mer51"
#define PACKAGE_VERSION "0.86.0+mer52"

/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

/* Version number of package */
#define VERSION "0.86.0+mer51"
#define VERSION "0.86.0+mer52"

/* Define to empty if `const' does not conform to ANSI C. */
/* #undef const */
2 changes: 1 addition & 1 deletion configure.ac
@@ -1,4 +1,4 @@
AC_INIT([usb_moded], [0.86.0+mer51])
AC_INIT([usb_moded], [0.86.0+mer52])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AM_EXTRA_RECURSIVE_TARGETS([dbus-gmain])

Expand Down
3 changes: 2 additions & 1 deletion rpm/usb-moded.spec
@@ -1,5 +1,5 @@
Name: usb-moded
Version: 0.86.0+mer51
Version: 0.86.0+mer52
Release: 2
Summary: USB mode controller
License: LGPLv2
Expand All @@ -17,6 +17,7 @@ BuildRequires: pkgconfig(ssu-sysinfo)
BuildRequires: pkgconfig(dsme) >= 0.65.0
BuildRequires: pkgconfig(sailfishaccesscontrol)
BuildRequires: systemd
BuildRequires: libtool

Requires: lsof
Requires: usb-moded-configs
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -52,7 +52,9 @@ usb_moded_SOURCES = \
usb_moded-sigpipe.h \
usb_moded-sigpipe.c \
usb_moded-control.h \
usb_moded-control.c
usb_moded-control.c \
usb_moded-user.h \
usb_moded-user.c

if USE_MER_SSU
usb_moded_SOURCES += \
Expand Down
74 changes: 64 additions & 10 deletions src/usb_moded-control.c
Expand Up @@ -30,6 +30,7 @@
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-worker.h"
#include "usb_moded-user.h"

#include <string.h>
#include <stdlib.h>
Expand All @@ -52,6 +53,7 @@
* ------------------------------------------------------------------------- */

void control_rethink_usb_charging_fallback(void);
void control_user_changed (void);
const char *control_get_external_mode (void);
static void control_set_external_mode (const char *mode);
void control_clear_external_mode (void);
Expand All @@ -64,11 +66,14 @@ void control_clear_internal_mode (void);
void control_set_usb_mode (const char *mode);
void control_mode_switched (const char *mode);
void control_select_usb_mode (void);
void control_select_usb_mode_ex (bool user_changed);
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);
uid_t control_get_current_user (void);
uid_t control_get_user_for_mode (void);
void control_set_user_for_mode (uid_t uid);

/* ========================================================================= *
* Data
Expand Down Expand Up @@ -100,10 +105,56 @@ static char *control_internal_mode = NULL;
*/
static cable_state_t control_cable_state = CABLE_STATE_UNKNOWN;

/** Uid of the user that has set current USB mode
*/
static uid_t control_user_for_mode = UID_UNKNOWN;

/* ========================================================================= *
* Functions
* ========================================================================= */

/** Get uid of the user that set the current mode
*
* @return uid of user when current mode was set
*/
uid_t
control_get_user_for_mode(void)
{
return control_user_for_mode;
}

/** Set the uid of the user that set the current mode
*
* @param uid of current user
*/
void
control_set_user_for_mode(uid_t uid)
{
LOG_REGISTER_CONTEXT;

log_debug("control_user_for_mode: %d -> %d",
(int)control_user_for_mode, (int)uid);
control_user_for_mode = uid;
}

/** Check if we can/should enable charging fallback mode
*
* Called when user is changed
*/
void
control_user_changed(void)
{
LOG_REGISTER_CONTEXT;

/* Cable must be connected to a pc */
if( control_get_cable_state() != CABLE_STATE_PC_CONNECTED )
return;
bool user_changed = (control_get_current_user() != control_get_user_for_mode());
log_debug("control_user_changed: user_changed %d", user_changed);
if (user_changed)
control_select_usb_mode_ex(user_changed);
}

/** Check if we can/should leave charging fallback mode
*
* Called when device lock status, or device status (dsme)
Expand Down Expand Up @@ -281,6 +332,9 @@ void control_set_usb_mode(const char *mode)
/* Invalidate current mode for the duration of mode transition */
control_set_external_mode(MODE_BUSY);

/* Set mode owner to unknown until it has been changed */
control_set_user_for_mode(UID_UNKNOWN);

/* Propagate down to gadget config */
worker_request_hardware_mode(control_internal_mode);

Expand All @@ -307,6 +361,7 @@ void control_mode_switched(const char *mode)

/* Propagate up to D-Bus */
control_update_external_mode();
control_set_user_for_mode(control_get_current_user());

return;
}
Expand All @@ -316,7 +371,7 @@ void control_mode_switched(const char *mode)
* gauge what mode to enter and then call control_set_usb_mode()
*
*/
void control_select_usb_mode(void)
void control_select_usb_mode_ex(bool user_changed)
{
LOG_REGISTER_CONTEXT;

Expand Down Expand Up @@ -366,20 +421,25 @@ void control_select_usb_mode(void)
}
}

if( mode_to_set && usbmoded_can_export() ) {
if( mode_to_set && usbmoded_can_export() && !user_changed ) {
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.
* export the system contents, if we are in acting dead mode or changing user.
*/
control_set_usb_mode(MODE_CHARGING_FALLBACK);
}
EXIT:
free(mode_to_set);
}

void control_select_usb_mode(void)
{
control_select_usb_mode_ex(false);
}

/** set the usb connection status
*
* @param cable_state CABLE_STATE_DISCONNECTED, ...
Expand Down Expand Up @@ -463,11 +523,5 @@ bool control_get_connection_state(void)
*/
uid_t control_get_current_user(void)
{
#ifdef SAILFISH_ACCESS_CONTROL
uid_t uid = UID_UNKNOWN;
sd_seat_get_active("seat0", 0, &uid);
return uid;
#else
return 0;
#endif
return user_get_current_user();
}
1 change: 1 addition & 0 deletions src/usb_moded-control.h
Expand Up @@ -37,6 +37,7 @@
* ------------------------------------------------------------------------- */

void control_rethink_usb_charging_fallback(void);
void control_user_changed (void);
const char *control_get_external_mode (void);
void control_clear_external_mode (void);
const char *control_get_target_mode (void);
Expand Down

0 comments on commit f366b46

Please sign in to comment.