From 8abafa0d959ae9441336db852246a5ee712b32f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Lepp=C3=A4nen?= Date: Wed, 15 Jan 2020 14:31:37 +0200 Subject: [PATCH] [control] Limit available modes with last seen user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add method to set last seen user. Use that information to limit the implicitly set modes. If no user has been seen yet, fallback to charging only mode. Signed-off-by: Tomi Leppänen --- src/usb_moded-control.c | 31 ++++++++++++++++++++++++++----- src/usb_moded-control.h | 1 + 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/usb_moded-control.c b/src/usb_moded-control.c index a9cbbc3..88ad301 100644 --- a/src/usb_moded-control.c +++ b/src/usb_moded-control.c @@ -58,6 +58,7 @@ 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); +void control_set_last_seen_user (uid_t uid); /* ========================================================================= * * Data @@ -89,6 +90,12 @@ static char *control_internal_mode = NULL; */ static cable_state_t control_cable_state = CABLE_STATE_UNKNOWN; +/** Last user seen + * + * Defaults to invalid user which has no rights. + */ +static uid_t control_last_seen_user = (uid_t)-1; + /* ========================================================================= * * Functions * ========================================================================= */ @@ -337,12 +344,17 @@ void control_select_usb_mode(void) /* 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, 0); - if( *available && !strchr(available, ',') ) { - free(mode_to_set), mode_to_set = available, available = 0; + if( control_last_seen_user == (uid_t)-1 ) { + /* Use charging only if no user has been seen */ + free(mode_to_set), mode_to_set = 0; + } else { + // FIXME free() vs g_free() conflict + gchar *available = common_get_mode_list(AVAILABLE_MODES_LIST, control_last_seen_user); + if( *available && !strchr(available, ',') ) { + free(mode_to_set), mode_to_set = available, available = 0; + } + g_free(available); } - g_free(available); } if( mode_to_set && usbmoded_can_export() ) { @@ -431,3 +443,12 @@ bool control_get_connection_state(void) } return connected; } + +/** Set the last seen user + * + * @param uid of last seen user, controls implicitly set modes + */ +void control_set_last_seen_user(uid_t uid) +{ + control_last_seen_user = uid; +} diff --git a/src/usb_moded-control.h b/src/usb_moded-control.h index 4e50ccf..2d33a16 100644 --- a/src/usb_moded-control.h +++ b/src/usb_moded-control.h @@ -49,5 +49,6 @@ 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); +void control_set_last_seen_user (uid_t uid); #endif /* USB_MODED_CONTROL_H_ */