Commit 50370d44 authored by spiiroin's avatar spiiroin

Merge branch 'jb42757_worker_thread' into 'master'

Move synchronous mode switch activity to worker thread

See merge request mer-core/usb-moded!37
parents 461747e7 054e8e00
This diff is collapsed.
......@@ -169,10 +169,13 @@ LDLIBS += $(PKG_LDLIBS)
# ----------------------------------------------------------------------------
usb_moded-OBJS += src/usb_moded.o
usb_moded-OBJS += src/usb_moded-android.o
usb_moded-OBJS += src/usb_moded-appsync.o
usb_moded-OBJS += src/usb_moded-common.o
usb_moded-OBJS += src/usb_moded-config.o
usb_moded-OBJS += src/usb_moded-configfs.o
usb_moded-OBJS += src/usb_moded-control.o
usb_moded-OBJS += src/usb_moded-dbus.o
usb_moded-OBJS += src/usb_moded-devicelock.o
usb_moded-OBJS += src/usb_moded-dsme.o
......@@ -182,10 +185,12 @@ usb_moded-OBJS += src/usb_moded-mac.o
usb_moded-OBJS += src/usb_moded-modesetting.o
usb_moded-OBJS += src/usb_moded-modules.o
usb_moded-OBJS += src/usb_moded-network.o
usb_moded-OBJS += src/usb_moded-sigpipe.o
usb_moded-OBJS += src/usb_moded-ssu.o
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 : $(usb_moded-OBJS)
$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
......@@ -217,8 +222,10 @@ usb_moded_util : $(usb_moded_util-OBJS)
CLEAN_SOURCES += src/usb_moded-android.c
CLEAN_SOURCES += src/usb_moded-appsync-dbus.c
CLEAN_SOURCES += src/usb_moded-appsync.c
CLEAN_SOURCES += src/usb_moded-common.c
CLEAN_SOURCES += src/usb_moded-config.c
CLEAN_SOURCES += src/usb_moded-configfs.c
CLEAN_SOURCES += src/usb_moded-control.c
CLEAN_SOURCES += src/usb_moded-dbus.c
CLEAN_SOURCES += src/usb_moded-devicelock.c
CLEAN_SOURCES += src/usb_moded-dsme.c
......@@ -228,11 +235,13 @@ CLEAN_SOURCES += src/usb_moded-mac.c
CLEAN_SOURCES += src/usb_moded-modesetting.c
CLEAN_SOURCES += src/usb_moded-modules.c
CLEAN_SOURCES += src/usb_moded-network.c
CLEAN_SOURCES += src/usb_moded-sigpipe.c
CLEAN_SOURCES += src/usb_moded-ssu.c
CLEAN_SOURCES += src/usb_moded-systemd.c
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.c
CLEAN_SOURCES += utils/udev-search.c
......@@ -241,8 +250,10 @@ CLEAN_HEADERS += src/usb_moded-appsync-dbus-private.h
CLEAN_HEADERS += src/usb_moded-appsync-dbus.h
CLEAN_HEADERS += src/usb_moded-appsync.h
CLEAN_HEADERS += src/usb_moded-config-private.h
CLEAN_HEADERS += src/usb_moded-common.h
CLEAN_HEADERS += src/usb_moded-config.h
CLEAN_HEADERS += src/usb_moded-configfs.h
CLEAN_HEADERS += src/usb_moded-control.h
CLEAN_HEADERS += src/usb_moded-dbus-private.h
CLEAN_HEADERS += src/usb_moded-dbus.h
CLEAN_HEADERS += src/usb_moded-devicelock.h
......@@ -254,10 +265,12 @@ CLEAN_HEADERS += src/usb_moded-modes.h
CLEAN_HEADERS += src/usb_moded-modesetting.h
CLEAN_HEADERS += src/usb_moded-modules.h
CLEAN_HEADERS += src/usb_moded-network.h
CLEAN_HEADERS += src/usb_moded-sigpipe.h
CLEAN_HEADERS += src/usb_moded-ssu.h
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.h
# Files with whitespace issues
......@@ -326,3 +339,19 @@ clean::
distclean::
$(RM) -r RPMS installroot
.SUFFIXES: %.trim
.PRECIOUS: %.trim
include_trim:: include_trim_headers include_trim_sources
%.trim : %
find_unneeded_includes.py $(CPPFLAGS) $(CFLAGS) -- $<
touch $@
include_trim_headers: $(patsubst %,%.trim,$(ALL_HEADERS))
include_trim_sources: $(patsubst %,%.trim,$(ALL_SOURCES))
distclean::
$(RM) *.trim */*.trim
USB_MODED_CFLAGS += -D_GNU_SOURCE
USB_MODED_LIBS += -lpthread
sbin_PROGRAMS = usb_moded \
usb_moded_util
......@@ -24,6 +25,8 @@ usb_moded_SOURCES = \
usb_moded-modules.h \
usb_moded-log.h \
usb_moded-log.c \
usb_moded-common.c \
usb_moded-common.h \
usb_moded-config.c \
usb_moded-config.h \
usb_moded-network.c \
......@@ -39,8 +42,14 @@ usb_moded_SOURCES = \
usb_moded-modules.c \
usb_moded-configfs.c \
usb_moded-configfs.h \
usb_moded-worker.h \
usb_moded-worker.c \
usb_moded-android.h \
usb_moded-android.c
usb_moded-android.c \
usb_moded-sigpipe.h \
usb_moded-sigpipe.c \
usb_moded-control.h \
usb_moded-control.c
if USE_MER_SSU
usb_moded_SOURCES += \
......
......@@ -21,19 +21,17 @@
* 02110-1301 USA
*/
#include <stdio.h>
#include <glib.h>
#include "usb_moded.h"
#include "usb_moded-android.h"
#include "usb_moded-log.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-config-private.h"
#include "usb_moded-log.h"
#include "usb_moded-mac.h"
#include "usb_moded-modesetting.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* ========================================================================= *
* Functions
......
......@@ -25,6 +25,7 @@
# define USB_MODED_ANDROID_H_
# include <stdbool.h>
# include <glib.h>
/* ========================================================================= *
* Constants
......
......@@ -25,7 +25,9 @@
#ifndef USB_MODED_APPSYNC_DBUS_PRIVATE_H_
# define USB_MODED_APPSYNC_DBUS_PRIVATE_H_
#include "usb_moded-appsync-dbus.h"
# include "usb_moded-appsync-dbus.h" // NOTRIM
# include <glib.h>
/* ========================================================================= *
* Prototypes
......
......@@ -25,20 +25,14 @@
* 02110-1301 USA
*/
#include <stdio.h>
#include "usb_moded-appsync-dbus-private.h"
#include "usb_moded-appsync.h"
#include "usb_moded-log.h"
#include <string.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "usb_moded-dbus-private.h"
#include "usb_moded.h"
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-config-private.h"
#include "usb_moded-appsync.h"
#include "usb_moded-appsync-dbus-private.h"
/* ========================================================================= *
* Prototypes
......@@ -183,7 +177,7 @@ static DBusHandlerResult dbusappsync_msg_handler(DBusConnection *const connectio
// could not parse method call args
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
}
else if( appsync_mark_active(use) < 0 )
else if( appsync_mark_active(use, 1) < 0 )
{
// name could not be marked active
reply = dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, member);
......
This diff is collapsed.
......@@ -26,6 +26,8 @@
#ifndef USB_MODED_APPSYNC_H_
# define USB_MODED_APPSYNC_H_
# include <glib.h>
/* ========================================================================= *
* Constants
* ========================================================================= */
......@@ -46,7 +48,7 @@
/** Application activation state
*/
typedef enum {
typedef enum app_state_t {
/** Application is not relevant for the current mode */
APP_STATE_DONTCARE = 0,
/** Application should be started */
......@@ -58,7 +60,7 @@ typedef enum {
/**
* keep all the needed info together for launching an app
*/
typedef struct list_elem
typedef struct list_elem_t
{
char *name; /**< name of the app to launch */
char *mode; /**< mode in which to launch the app */
......@@ -66,7 +68,7 @@ typedef struct list_elem
app_state_t state; /**< marker to check if the app has started sucessfully */
int systemd; /**< marker to know if we start it with systemd or not */
int post; /**< marker to indicate when to start the app */
} list_elem;
} list_elem_t;
/* ========================================================================= *
* Prototypes
......
This diff is collapsed.
#ifndef USB_MODED_COMMON_H_
# define USB_MODED_COMMON_H_
# include <stdio.h>
# include <stdbool.h>
# include <glib.h>
/* ========================================================================= *
* Types
* ========================================================================= */
/** Mode list types
*/
typedef enum mode_list_type_t {
/** All configured modes */
SUPPORTED_MODES_LIST,
/** Configured modes that can be activated */
AVAILABLE_MODES_LIST
} mode_list_type_t;
typedef enum {
CABLE_STATE_UNKNOWN,
CABLE_STATE_DISCONNECTED,
CABLE_STATE_CHARGER_CONNECTED,
CABLE_STATE_PC_CONNECTED,
CABLE_STATE_NUMOF
} cable_state_t;
typedef enum waitres_t
{
WAIT_FAILED,
WAIT_READY,
WAIT_TIMEOUT,
} waitres_t;
/* ========================================================================= *
* Functions
* ========================================================================= */
/* -- cable -- */
const char *cable_state_repr(cable_state_t state);
/* -- common -- */
const char *common_map_mode_to_hardware (const char *internal_mode);
const char *common_map_mode_to_external (const char *internal_mode);
void common_send_supported_modes_signal (void);
void common_send_available_modes_signal (void);
void common_send_hidden_modes_signal (void);
void common_send_whitelisted_modes_signal(void);
void common_acquire_wakelock (const char *wakelock_name);
void common_release_wakelock (const char *wakelock_name);
int common_system_ (const char *file, int line, const char *func, const char *command);
FILE *common_popen_ (const char *file, int line, const char *func, const char *command, const char *type);
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);
int common_valid_mode (const char *mode);
gchar *common_get_mode_list (mode_list_type_t type);
/* ========================================================================= *
* Macros
* ========================================================================= */
# define common_system(command) common_system_(__FILE__,__LINE__,__FUNCTION__,(command))
# define common_popen(command, type) common_popen_(__FILE__,__LINE__,__FUNCTION__,(command),(type))
# define common_msleep(msec) common_msleep_(__FILE__,__LINE__,__FUNCTION__,(msec))
# define common_sleep(sec) common_msleep_(__FILE__,__LINE__,__FUNCTION__,(sec)*1000)
#endif /* USB_MODED_COMMON_H_ */
......@@ -36,6 +36,8 @@
# include "usb_moded-config.h"
# include <glib.h>
/* ========================================================================= *
* Prototypes
* ========================================================================= */
......
This diff is collapsed.
......@@ -22,21 +22,21 @@
#include "usb_moded-configfs.h"
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <glib.h>
#include "usb_moded.h"
#include "usb_moded-android.h"
#include "usb_moded-log.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-common.h"
#include "usb_moded-config-private.h"
#include "usb_moded-log.h"
#include "usb_moded-mac.h"
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
/* ========================================================================= *
* Constants
* ========================================================================= */
......@@ -598,7 +598,7 @@ configfs_init_values(void)
/* Prep: mtp_mode */
configfs_register_function(FUNCTION_MTP);
if( access("/dev/mtp/ep0", F_OK) == -1 ) {
usbmoded_system("/bin/mount -o uid=100000,gid=100000 -t functionfs mtp /dev/mtp");
common_system("/bin/mount -o uid=100000,gid=100000 -t functionfs mtp /dev/mtp");
}
/* Prep: developer_mode */
......
/**
* @file usb_moded-control.c
*
* Copyright (C) 2013-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "usb_moded-control.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 <string.h>
#include <stdlib.h>
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- usbmoded -- */
void control_rethink_usb_charging_fallback(void);
const char *control_get_external_mode (void);
static void control_set_external_mode (const char *mode);
void control_clear_external_mode (void);
static void control_update_external_mode (void);
const char *control_get_usb_mode (void);
void control_clear_internal_mode (void);
void control_set_usb_mode (const char *mode);
void control_mode_switched (const char *override);
void control_select_usb_mode (void);
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);
/* ========================================================================= *
* Data
* ========================================================================= */
/* The external mode;
*
* What was the last mode signaled over D-Bus.
*/
static char *control_external_mode = NULL;
/** The logical mode name
*
* Full set of valid modes can occur here
*/
static char *control_internal_mode = NULL;
/** Connection status
*
* Access only via:
* - control_set_cable_state()
* - control_get_connection_state()
*/
static cable_state_t control_cable_state = CABLE_STATE_UNKNOWN;
/* ========================================================================= *
* Functions
* ========================================================================= */
/** Check if we can/should leave charging fallback mode
*
* Called when device lock status, or device status (dsme)
* changes.
*/
void
control_rethink_usb_charging_fallback(void)
{
/* Cable must be connected to a pc */
if( control_get_cable_state() != CABLE_STATE_PC_CONNECTED )
goto EXIT;
/* Switching can happen only from MODE_UNDEFINED
* or MODE_CHARGING_FALLBACK */
const char *usb_mode = control_get_usb_mode();
if( strcmp(usb_mode, MODE_UNDEFINED) &&
strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
goto EXIT;
if( !usbmoded_can_export() ) {
log_notice("exporting data not allowed; stay in %s", usb_mode);
goto EXIT;
}
log_debug("attempt to leave %s", usb_mode);
control_select_usb_mode();
EXIT:
return;
}
const char *control_get_external_mode(void)
{
return control_external_mode ?: MODE_UNDEFINED;
}
static void control_set_external_mode(const char *mode)
{
gchar *previous = control_external_mode;
if( !g_strcmp0(previous, mode) )
goto EXIT;
log_debug("external_mode: %s -> %s",
previous, mode);
control_external_mode = g_strdup(mode);
g_free(previous);
// DO THE DBUS BROADCAST
if( !strcmp(control_external_mode, MODE_ASK) ) {
/* send signal, mode will be set when the dialog service calls
* the set_mode method call. */
umdbus_send_state_signal(USB_CONNECTED_DIALOG_SHOW);
}
umdbus_send_state_signal(control_external_mode);
EXIT:
return;
}
void control_clear_external_mode(void)
{
g_free(control_external_mode),
control_external_mode = 0;
}
static void control_update_external_mode(void)
{
const char *internal_mode = control_get_usb_mode();
const char *external_mode = common_map_mode_to_external(internal_mode);
control_set_external_mode(external_mode);
}
/** get the usb mode
*
* @return the currently set mode
*
*/
const char * control_get_usb_mode(void)
{
return control_internal_mode;
}
void control_clear_internal_mode(void)
{
g_free(control_internal_mode),
control_internal_mode = 0;
}
/** set the usb mode
*
* @param mode The requested USB mode
*/
void control_set_usb_mode(const char *mode)
{
gchar *previous = control_internal_mode;
if( !g_strcmp0(previous, mode) )
goto EXIT;
log_debug("internal_mode: %s -> %s",
previous, mode);
control_internal_mode = g_strdup(mode);
g_free(previous);
/* Invalidate current mode for the duration of mode transition */
control_set_external_mode(MODE_BUSY);
/* Propagate down to gadget config */
worker_request_hardware_mode(control_internal_mode);
EXIT:
return;
}
/* Worker thread has finished mode switch
*
* @param mode The activated USB mode
*/
void control_mode_switched(const char *mode)
{
/* Update state data - without retriggering the worker thread
*/
if( g_strcmp0(control_internal_mode, mode) ) {
log_debug("internal_mode: %s -> %s",
control_internal_mode, mode);
g_free(control_internal_mode),
control_internal_mode = g_strdup(mode);
}
/* Propagate up to D-Bus */
control_update_external_mode();
return;
}
/** set the chosen usb state
*
* gauge what mode to enter and then call control_set_usb_mode()
*
*/
void control_select_usb_mode(void)
{
char *mode_to_set = 0;
if( usbmoded_get_rescue_mode() ) {
log_debug("Entering rescue mode!\n");
control_set_usb_mode(MODE_DEVELOPER);
goto EXIT;
}
if( usbmoded_get_diag_mode() ) {
/* Assumption is that in diag-mode there is only
* one mode configured i.e. list head is diag-mode. */
GList *iter = usbmoded_get_modelist();
if( !iter ) {
log_err("Diagnostic mode is not configured!");
}
else {
mode_list_elem_t *data = iter->data;
log_debug("Entering diagnostic mode!");
control_set_usb_mode(data->mode_name);
}
goto EXIT;
}
mode_to_set = config_get_mode_setting();
/* 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);
if( *available && !strchr(available, ',') ) {
free(mode_to_set), mode_to_set = available, available = 0;
}
g_free(available);
}
if( mode_to_set && usbmoded_can_export() ) {
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.
*/
control_set_usb_mode(MODE_CHARGING_FALLBACK);
}
EXIT:
free(mode_to_set);
}
/** set the usb connection status
*
* @param cable_state CABLE_STATE_DISCONNECTED, ...
*/
void control_set_cable_state(cable_state_t cable_state)
{
cable_state_t prev = control_cable_state;
control_cable_state = cable_state;
if( control_cable_state == prev )
goto EXIT;
log_debug("control_cable_state: %s -> %s",
cable_state_repr(prev),
cable_state_repr(control_cable_state));
switch( control_cable_state ) {
default:
case CABLE_STATE_DISCONNECTED:
control_set_usb_mode(MODE_UNDEFINED);
break;
case CABLE_STATE_CHARGER_CONNECTED:
control_set_usb_mode(MODE_CHARGER);
break;
case CABLE_STATE_PC_CONNECTED:
control_select_usb_mode();
break;
}
EXIT:
return;
}
/** get the usb connection status
*
* @return CABLE_STATE_DISCONNECTED, ...
*/
cable_state_t control_get_cable_state(void)
{
return control_cable_state;
}
void control_clear_cable_state(void)
{
control_cable_state = CABLE_STATE_UNKNOWN;
}
/** Get if the cable (pc or charger) is connected or not
*
* @ return true if connected, false if disconnected
*/
bool control_get_connection_state(void)
{
bool connected = false;
switch( control_get_cable_state() ) {
case CABLE_STATE_CHARGER_CONNECTED:
case CABLE_STATE_PC_CONNECTED:
connected = true;
break;
default:
break;
}
return connected;
}
/**
* @file usb_moded-control.h
*
* Copyright (C) 2013-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#ifndef USB_MODED_CONTROL_H_
# define USB_MODED_CONTROL_H_
# include "usb_moded.h"
# include "usb_moded-common.h"
/* -- usbmoded -- */
void control_rethink_usb_charging_fallback(void);
const char *control_get_external_mode (void);
void control_clear_external_mode (void);
const char *control_get_usb_mode (void);
void control_clear_internal_mode (void);