Skip to content

Commit

Permalink
[usb_moded] Implement and use common mode selection logic. Fixes JB#5…
Browse files Browse the repository at this point in the history
…3122

When device is booted up with cable connected and device lock code is
not used, usb-mode is left at fallback_charging. This happens because
usb-mode is re-evaluated on device unlock event, which does not occur
when device lock is not used. Due to duplication of mode selection
logic in various notification handlers, similar hard to find corner
cases are likely to occur also when order of events is not as
expected.

Collect all mode selection logic into a single function and refactor
control flow so that all triggers that might require a mode change are
directed there.

Additional changes to existing logic:
- All mode changes are blocked until usb-moded enters mainloop
- When shutdown is initiated, dynamic mode changes are blocked
- Dynamic modes are canceled when user change is started, and
- Re-evaluated / activated after user change is finished
- Exception: already active rescue mode persists over user changes

Reduce the amount of conditional MEEGOLOCK code by adding abstract
helper functions in usb_moded.c and using those instead of functions
that might not be available based on configuration options.

Clean up unnecessary header file includes resulting from these and
earlier changes.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jolla.com>
  • Loading branch information
spiiroin committed Mar 2, 2021
1 parent 758e080 commit 3f3d04f
Show file tree
Hide file tree
Showing 30 changed files with 775 additions and 393 deletions.
62 changes: 16 additions & 46 deletions .depend
Expand Up @@ -110,29 +110,23 @@ src/usb_moded-config.pic.o:\

src/usb_moded-configfs.o:\
src/usb_moded-configfs.c\
config-static.h\
src/usb_moded-android.h\
src/usb_moded-common.h\
src/usb_moded-config-private.h\
src/usb_moded-config.h\
src/usb_moded-configfs.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-mac.h\
src/usb_moded.h\

src/usb_moded-configfs.pic.o:\
src/usb_moded-configfs.c\
config-static.h\
src/usb_moded-android.h\
src/usb_moded-common.h\
src/usb_moded-config-private.h\
src/usb_moded-config.h\
src/usb_moded-configfs.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-mac.h\
src/usb_moded.h\

src/usb_moded-control.o:\
src/usb_moded-control.c\
Expand Down Expand Up @@ -198,53 +192,41 @@ src/usb_moded-dbus.pic.o:\

src/usb_moded-devicelock.o:\
src/usb_moded-devicelock.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-dbus-private.h\
src/usb_moded-dbus.h\
src/usb_moded-devicelock.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded.h\

src/usb_moded-devicelock.pic.o:\
src/usb_moded-devicelock.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-dbus-private.h\
src/usb_moded-dbus.h\
src/usb_moded-devicelock.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded.h\

src/usb_moded-dsme.o:\
src/usb_moded-dsme.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-dbus-private.h\
src/usb_moded-dbus.h\
src/usb_moded-dsme.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-modesetting.h\
src/usb_moded.h\

src/usb_moded-dsme.pic.o:\
src/usb_moded-dsme.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-dbus-private.h\
src/usb_moded-dbus.h\
src/usb_moded-dsme.h\
src/usb_moded-dyn-config.h\
src/usb_moded-log.h\
src/usb_moded-modesetting.h\
src/usb_moded.h\

src/usb_moded-dyn-config.o:\
src/usb_moded-dyn-config.c\
Expand Down Expand Up @@ -276,7 +258,6 @@ src/usb_moded-mac.pic.o:\

src/usb_moded-modesetting.o:\
src/usb_moded-modesetting.c\
config-static.h\
src/usb_moded-android.h\
src/usb_moded-appsync.h\
src/usb_moded-common.h\
Expand All @@ -291,11 +272,9 @@ src/usb_moded-modesetting.o:\
src/usb_moded-modules.h\
src/usb_moded-network.h\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded-modesetting.pic.o:\
src/usb_moded-modesetting.c\
config-static.h\
src/usb_moded-android.h\
src/usb_moded-appsync.h\
src/usb_moded-common.h\
Expand All @@ -310,7 +289,6 @@ src/usb_moded-modesetting.pic.o:\
src/usb_moded-modules.h\
src/usb_moded-network.h\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded-modules.o:\
src/usb_moded-modules.c\
Expand All @@ -324,7 +302,6 @@ src/usb_moded-modules.pic.o:\

src/usb_moded-network.o:\
src/usb_moded-network.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-config-private.h\
src/usb_moded-config.h\
Expand All @@ -336,11 +313,9 @@ src/usb_moded-network.o:\
src/usb_moded-modesetting.h\
src/usb_moded-network.h\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded-network.pic.o:\
src/usb_moded-network.c\
config-static.h\
src/usb_moded-common.h\
src/usb_moded-config-private.h\
src/usb_moded-config.h\
Expand All @@ -352,7 +327,6 @@ src/usb_moded-network.pic.o:\
src/usb_moded-modesetting.h\
src/usb_moded-network.h\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded-sigpipe.o:\
src/usb_moded-sigpipe.c\
Expand Down Expand Up @@ -446,6 +420,20 @@ src/usb_moded-udev.pic.o:\
src/usb_moded-udev.h\
src/usb_moded.h\

src/usb_moded-user.o:\
src/usb_moded-user.c\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-log.h\
src/usb_moded-user.h\

src/usb_moded-user.pic.o:\
src/usb_moded-user.c\
src/usb_moded-common.h\
src/usb_moded-control.h\
src/usb_moded-log.h\
src/usb_moded-user.h\

src/usb_moded-util.o:\
src/usb_moded-util.c\
src/usb_moded-dbus-private.h\
Expand Down Expand Up @@ -488,22 +476,6 @@ 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 @@ -524,13 +496,12 @@ src/usb_moded.o:\
src/usb_moded-modes.h\
src/usb_moded-modesetting.h\
src/usb_moded-modules.h\
src/usb_moded-network.h\
src/usb_moded-sigpipe.h\
src/usb_moded-systemd.h\
src/usb_moded-trigger.h\
src/usb_moded-udev.h\
src/usb_moded-worker.h\
src/usb_moded-user.h\
src/usb_moded-worker.h\
src/usb_moded.h\

src/usb_moded.pic.o:\
Expand All @@ -553,13 +524,12 @@ src/usb_moded.pic.o:\
src/usb_moded-modes.h\
src/usb_moded-modesetting.h\
src/usb_moded-modules.h\
src/usb_moded-network.h\
src/usb_moded-sigpipe.h\
src/usb_moded-systemd.h\
src/usb_moded-trigger.h\
src/usb_moded-udev.h\
src/usb_moded-worker.h\
src/usb_moded-user.h\
src/usb_moded-worker.h\
src/usb_moded.h\

utils/udev-search.o:\
Expand Down
4 changes: 1 addition & 3 deletions src/usb_moded-android.c
@@ -1,7 +1,7 @@
/**
* @file usb_moded-android.c
*
* Copyright (c) 2013 - 2020 Jolla Ltd.
* Copyright (c) 2013 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.deswert@jollamobile.com>
Expand Down Expand Up @@ -31,8 +31,6 @@

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* ========================================================================= *
* Prototypes
Expand Down
4 changes: 1 addition & 3 deletions src/usb_moded-appsync-dbus.c
Expand Up @@ -2,7 +2,7 @@
* @file usb_moded-appsync-dbus.c
*
* Copyright (c) 2010 Nokia Corporation. All rights reserved.
* Copyright (c) 2013 - 2020 Jolla Ltd.
* Copyright (c) 2013 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.de-swert@nokia.com>
Expand Down Expand Up @@ -31,8 +31,6 @@
#include "usb_moded-appsync.h"
#include "usb_moded-log.h"

#include <string.h>

#include <dbus/dbus.h>

/* ========================================================================= *
Expand Down
6 changes: 1 addition & 5 deletions src/usb_moded-appsync.c
Expand Up @@ -2,7 +2,7 @@
* @file usb_moded-appsync.c
*
* Copyright (c) 2010 Nokia Corporation. All rights reserved.
* Copyright (c) 2013 - 2020 Jolla Ltd.
* Copyright (c) 2013 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.de-swert@nokia.com>
Expand Down Expand Up @@ -33,10 +33,6 @@
#include "usb_moded-log.h"
#include "usb_moded-systemd.h"

#include <sys/time.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glob.h>

Expand Down
27 changes: 19 additions & 8 deletions src/usb_moded-common.c
Expand Up @@ -2,7 +2,7 @@
* @file usb_moded-common.c
*
* Copyright (c) 2010 Nokia Corporation. All rights reserved.
* Copyright (c) 2012 - 2020 Jolla Ltd.
* Copyright (c) 2012 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.de-swert@nokia.com>
Expand All @@ -29,18 +29,14 @@
#include "usb_moded.h"
#include "usb_moded-config-private.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-dyn-config.h"
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-worker.h"

#include <sys/wait.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

/* ========================================================================= *
* Types
Expand Down Expand Up @@ -88,6 +84,7 @@ waitres_t common_wait (unsigned tot_ms, bool (*ready_
bool common_msleep_ (const char *file, int line, const char *func, unsigned msec);
static bool common_mode_in_list (const char *mode, char *const *modes);
bool common_modename_is_internal (const char *modename);
bool common_modename_is_static (const char *modename);
int common_valid_mode (const char *mode);
gchar *common_get_mode_list (mode_list_type_t type, uid_t uid);

Expand Down Expand Up @@ -507,12 +504,26 @@ common_modename_is_internal(const char *modename)
{
LOG_REGISTER_CONTEXT;

return (common_modename_is_static(modename) ||
!g_strcmp0(modename, MODE_ASK) ||
!g_strcmp0(modename, MODE_BUSY));
}

/** Check if given usb mode is static
*
* @param modename name of a more
*
* @return true if mode is static, false otherwise
*/
bool
common_modename_is_static(const char *modename)
{
LOG_REGISTER_CONTEXT;

return (!g_strcmp0(modename, MODE_UNDEFINED) ||
!g_strcmp0(modename, MODE_CHARGER) ||
!g_strcmp0(modename, MODE_CHARGING_FALLBACK) ||
!g_strcmp0(modename, MODE_ASK) ||
!g_strcmp0(modename, MODE_CHARGING) ||
!g_strcmp0(modename, MODE_BUSY));
!g_strcmp0(modename, MODE_CHARGING));
}

/** check if a given usb_mode exists
Expand Down
3 changes: 2 additions & 1 deletion src/usb_moded-common.h
Expand Up @@ -2,7 +2,7 @@
* @file usb_moded-common.h
*
* Copyright (c) 2010 Nokia Corporation. All rights reserved.
* Copyright (c) 2012 - 2020 Jolla Ltd.
* Copyright (c) 2012 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.de-swert@nokia.com>
Expand Down Expand Up @@ -86,6 +86,7 @@ FILE *common_popen_ (const char *file, int line, con
waitres_t common_wait (unsigned tot_ms, bool (*ready_cb)(void *aptr), void *aptr);
bool common_msleep_ (const char *file, int line, const char *func, unsigned msec);
bool common_modename_is_internal (const char *modename);
bool common_modename_is_static (const char *modename);
int common_valid_mode (const char *mode);
gchar *common_get_mode_list (mode_list_type_t type, uid_t uid);

Expand Down

0 comments on commit 3f3d04f

Please sign in to comment.