Skip to content
Permalink
Browse files
[usb_moded] Use accessor functions for modelist
Makes it easier to differentiate between value use and value modify.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Sep 5, 2018
1 parent a8b0b33 commit a282313927928761aa5b4178c41d76ef9fef3154
Showing with 154 additions and 134 deletions.
  1. +70 −83 src/usb_moded-common.c
  2. +8 −4 src/usb_moded-control.c
  3. +1 −1 src/usb_moded-worker.c
  4. +58 −26 src/usb_moded.c
  5. +17 −20 src/usb_moded.h
@@ -397,117 +397,104 @@ static bool common_mode_in_list(const char *mode, char * const *modes)
/** check if a given usb_mode exists
*
* @param mode The mode to look for
* @return 0 if mode exists, 1 if it does not exist
*
* @return 0 if mode exists, 1 if it does not exist
*/
int common_valid_mode(const char *mode)
{
int valid = 1;
/* MODE_ASK, MODE_CHARGER and MODE_CHARGING_FALLBACK are not modes that are settable seen their special 'internal' status
* so we only check the modes that are announed outside. Only exception is the built in MODE_CHARGING */
if(!strcmp(MODE_CHARGING, mode))
if(!strcmp(MODE_CHARGING, mode)) {
valid = 0;
}
else
{
char *whitelist;
gchar **whitelist_split = NULL;

whitelist = config_get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
gchar *whitelist_value = 0;
gchar **whitelist_array = 0;

/* check dynamic modes */
if(usbmoded_modelist)
{
GList *iter;

for( iter = usbmoded_modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name))
{
if (!whitelist_split || common_mode_in_list(data->mode_name, whitelist_split))
valid = 0;
break;
}
}

g_strfreev(whitelist_split);
if( (whitelist_value = config_get_mode_whitelist()) )
whitelist_array = g_strsplit(whitelist_value, ",", 0);

for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) {
struct mode_list_elem *data = iter->data;
if( strcmp(mode, data->mode_name) )
continue;

if (!whitelist_array || common_mode_in_list(data->mode_name, whitelist_array))
valid = 0;
break;
}

g_strfreev(whitelist_array);
g_free(whitelist_value);
}
return valid;

}

/** make a list of all available usb modes
*
* @param type The type of list to return. Supported or available.
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
*
* @return a comma-separated list of modes (MODE_ASK not included as it is not a real mode)
*/
gchar *common_get_mode_list(mode_list_type_t type)
{
GString *modelist_str;
GString *mode_list_str = g_string_new(NULL);

modelist_str = g_string_new(NULL);
gchar *hidden_modes_value = 0;
gchar **hidden_modes_array = 0;

if(!usbmoded_get_diag_mode())
gchar *whitelist_value = 0;
gchar **whitelist_array = 0;

if( usbmoded_get_diag_mode() )
{
/* check dynamic modes */
if(usbmoded_modelist)
{
GList *iter;
char *hidden_modes_list, *whitelist;
gchar **hidden_mode_split = NULL, **whitelist_split = NULL;

hidden_modes_list = config_get_hidden_modes();
if(hidden_modes_list)
{
hidden_mode_split = g_strsplit(hidden_modes_list, ",", 0);
g_free(hidden_modes_list);
}

if (type == AVAILABLE_MODES_LIST)
{
whitelist = config_get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
}

for( iter = usbmoded_modelist; iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;

/* skip items in the hidden list */
if (common_mode_in_list(data->mode_name, hidden_mode_split))
continue;

/* if there is a whitelist skip items not in the list */
if (whitelist_split && !common_mode_in_list(data->mode_name, whitelist_split))
continue;

modelist_str = g_string_append(modelist_str, data->mode_name);
modelist_str = g_string_append(modelist_str, ", ");
}

g_strfreev(hidden_mode_split);
g_strfreev(whitelist_split);
}
/* diag mode. there is only one active mode */
g_string_append(mode_list_str, MODE_DIAG);
goto EXIT;
}

if( (hidden_modes_value = config_get_hidden_modes()) )
hidden_modes_array = g_strsplit(hidden_modes_value, ",", 0);

/* end with charging mode */
g_string_append(modelist_str, MODE_CHARGING);
return g_string_free(modelist_str, false);
switch( type ) {
case SUPPORTED_MODES_LIST:
/* All modes that are not hidden */
break;

case AVAILABLE_MODES_LIST:
/* All whitelisted modes that are not hidden */
if( (whitelist_value = config_get_mode_whitelist()) )
whitelist_array = g_strsplit(whitelist_value, ",", 0);
break;
}
else

for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) )
{
/* diag mode. there is only one active mode */
g_string_append(modelist_str, MODE_DIAG);
return g_string_free(modelist_str, false);
struct mode_list_elem *data = iter->data;

/* skip items in the hidden list */
if (common_mode_in_list(data->mode_name, hidden_modes_array))
continue;

/* if there is a whitelist skip items not in the list */
if (whitelist_array && !common_mode_in_list(data->mode_name, whitelist_array))
continue;

g_string_append(mode_list_str, data->mode_name);
g_string_append(mode_list_str, ", ");
}

/* End with charging mode */
g_string_append(mode_list_str, MODE_CHARGING);

EXIT:
g_strfreev(whitelist_array);
g_free(whitelist_value);

g_strfreev(hidden_modes_array);
g_free(hidden_modes_value);

return g_string_free(mode_list_str, false);
}
@@ -238,11 +238,15 @@ void control_select_usb_mode(void)
}

if( usbmoded_get_diag_mode() ) {
log_debug("Entering diagnostic mode!\n");
if( usbmoded_modelist ) {
/* XXX 1st entry is just assumed to be diag mode??? */
GList *iter = usbmoded_modelist;
/* 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 {
struct mode_list_elem *data = iter->data;
log_debug("Entering diagnostic mode!");
control_set_usb_mode(data->mode_name);
}
goto EXIT;
@@ -481,7 +481,7 @@ worker_switch_to_mode(const char *mode)
}

/* go through all the dynamic modes if the modelist exists*/
for( GList *iter = usbmoded_modelist; iter; iter = g_list_next(iter) )
for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) )
{
struct mode_list_elem *data = iter->data;
if( strcmp(mode, data->mode_name) )
@@ -104,31 +104,33 @@

/* -- usbmoded -- */

bool usbmoded_get_rescue_mode (void);
void usbmoded_set_rescue_mode (bool rescue_mode);
bool usbmoded_get_diag_mode (void);
void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_set_cable_connection_delay(int delay_ms);
int usbmoded_get_cable_connection_delay(void);
static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
bool usbmoded_can_export (void);
void usbmoded_exit_mainloop (int exitcode);
void usbmoded_handle_signal (int signum);
static bool usbmoded_init (void);
static void usbmoded_cleanup (void);
static void usbmoded_usage (void);
static void usbmoded_parse_options (int argc, char *argv[]);
GList *usbmoded_get_modelist (void);
void usbmoded_load_modelist (void);
void usbmoded_free_modelist (void);
bool usbmoded_get_rescue_mode (void);
void usbmoded_set_rescue_mode (bool rescue_mode);
bool usbmoded_get_diag_mode (void);
void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_set_cable_connection_delay(int delay_ms);
int usbmoded_get_cable_connection_delay(void);
static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
bool usbmoded_can_export (void);
void usbmoded_exit_mainloop (int exitcode);
void usbmoded_handle_signal (int signum);
static bool usbmoded_init (void);
static void usbmoded_cleanup (void);
static void usbmoded_usage (void);
static void usbmoded_parse_options (int argc, char *argv[]);

/* ========================================================================= *
* Data
* ========================================================================= */

GList *usbmoded_modelist = 0;
static int usbmoded_exitcode = EXIT_FAILURE;
static GMainLoop *usbmoded_mainloop = NULL;

@@ -141,6 +143,37 @@ static bool usbmoded_systemd_notify = false;
* Functions
* ========================================================================= */

/* ------------------------------------------------------------------------- *
* MODELIST
* ------------------------------------------------------------------------- */

static GList *usbmoded_modelist = 0;

GList *
usbmoded_get_modelist(void)
{
return usbmoded_modelist;
}

void
usbmoded_load_modelist(void)
{
if( !usbmoded_modelist ) {
log_notice("load modelist");
usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode());
}
}

void
usbmoded_free_modelist(void)
{
if( usbmoded_modelist ) {
log_notice("free modelist");
dynconfig_free_mode_list(usbmoded_modelist),
usbmoded_modelist = 0;
}
}

/* ------------------------------------------------------------------------- *
* RESCUE_MODE
* ------------------------------------------------------------------------- */
@@ -398,9 +431,8 @@ void usbmoded_handle_signal(int signum)
else if( signum == SIGHUP )
{
/* free and read in modelist again */
dynconfig_free_mode_list(usbmoded_modelist);

usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode());
usbmoded_free_modelist();
usbmoded_load_modelist();

common_send_supported_modes_signal();
common_send_available_modes_signal();
@@ -478,7 +510,7 @@ static bool usbmoded_init(void)
#endif

/* always read dyn modes even if appsync is not used */
usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode());
usbmoded_load_modelist();

if(config_check_trigger())
trigger_init();
@@ -613,8 +645,8 @@ static void usbmoded_cleanup(void)
/* Undo trigger_init() */
trigger_stop();

/* Undo dynconfig_read_mode_list() */
dynconfig_free_mode_list(usbmoded_modelist);
/* Undo usbmoded_load_modelist() */
usbmoded_free_modelist();

#ifdef APP_SYNC
/* Undo appsync_read_list() */
@@ -56,31 +56,28 @@
# define USB_MODED_SUSPEND_DELAY_MAXIMUM_MS \
(USB_MODED_SUSPEND_DELAY_DEFAULT_MS * 2)

/* ========================================================================= *
* Data
* ========================================================================= */

extern GList *usbmoded_modelist;

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

/* -- usbmoded -- */

bool usbmoded_get_rescue_mode (void);
void usbmoded_set_rescue_mode (bool rescue_mode);
bool usbmoded_get_diag_mode (void);
void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_set_cable_connection_delay(int delay_ms);
int usbmoded_get_cable_connection_delay(void);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
bool usbmoded_can_export (void);
void usbmoded_exit_mainloop (int exitcode);
void usbmoded_handle_signal (int signum);
GList *usbmoded_get_modelist (void);
void usbmoded_load_modelist (void);
void usbmoded_free_modelist (void);
bool usbmoded_get_rescue_mode (void);
void usbmoded_set_rescue_mode (bool rescue_mode);
bool usbmoded_get_diag_mode (void);
void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_set_cable_connection_delay(int delay_ms);
int usbmoded_get_cable_connection_delay(void);
void usbmoded_allow_suspend (void);
void usbmoded_delay_suspend (void);
bool usbmoded_init_done_p (void);
void usbmoded_set_init_done (bool reached);
void usbmoded_probe_init_done (void);
bool usbmoded_can_export (void);
void usbmoded_exit_mainloop (int exitcode);
void usbmoded_handle_signal (int signum);

#endif /* USB_MODED_H_ */

0 comments on commit a282313

Please sign in to comment.