Commit a2823139 authored by spiiroin's avatar spiiroin

[usb_moded] Use accessor functions for modelist

Makes it easier to differentiate between value use and value modify.
Signed-off-by: spiiroin's avatarSimo Piiroinen <simo.piiroinen@jollamobile.com>
parent a8b0b339
...@@ -397,117 +397,104 @@ static bool common_mode_in_list(const char *mode, char * const *modes) ...@@ -397,117 +397,104 @@ static bool common_mode_in_list(const char *mode, char * const *modes)
/** check if a given usb_mode exists /** check if a given usb_mode exists
* *
* @param mode The mode to look for * @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 common_valid_mode(const char *mode)
{ {
int valid = 1; int valid = 1;
/* MODE_ASK, MODE_CHARGER and MODE_CHARGING_FALLBACK are not modes that are settable seen their special 'internal' status /* 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 */ * 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; valid = 0;
}
else else
{ {
char *whitelist; gchar *whitelist_value = 0;
gchar **whitelist_split = NULL; gchar **whitelist_array = 0;
whitelist = config_get_mode_whitelist();
if (whitelist)
{
whitelist_split = g_strsplit(whitelist, ",", 0);
g_free(whitelist);
}
/* check dynamic modes */ if( (whitelist_value = config_get_mode_whitelist()) )
if(usbmoded_modelist) whitelist_array = g_strsplit(whitelist_value, ",", 0);
{
GList *iter; for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) {
struct mode_list_elem *data = iter->data;
for( iter = usbmoded_modelist; iter; iter = g_list_next(iter) ) if( strcmp(mode, data->mode_name) )
{ continue;
struct mode_list_elem *data = iter->data;
if(!strcmp(mode, data->mode_name)) if (!whitelist_array || common_mode_in_list(data->mode_name, whitelist_array))
{ valid = 0;
if (!whitelist_split || common_mode_in_list(data->mode_name, whitelist_split)) break;
valid = 0;
break;
}
}
g_strfreev(whitelist_split);
} }
g_strfreev(whitelist_array);
g_free(whitelist_value);
} }
return valid; return valid;
} }
/** make a list of all available usb modes /** make a list of all available usb modes
* *
* @param type The type of list to return. Supported or available. * @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) 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 */ /* diag mode. there is only one active mode */
if(usbmoded_modelist) g_string_append(mode_list_str, MODE_DIAG);
{ goto EXIT;
GList *iter; }
char *hidden_modes_list, *whitelist;
gchar **hidden_mode_split = NULL, **whitelist_split = NULL; if( (hidden_modes_value = config_get_hidden_modes()) )
hidden_modes_array = g_strsplit(hidden_modes_value, ",", 0);
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);
}
/* end with charging mode */ switch( type ) {
g_string_append(modelist_str, MODE_CHARGING); case SUPPORTED_MODES_LIST:
return g_string_free(modelist_str, false); /* 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 */ struct mode_list_elem *data = iter->data;
g_string_append(modelist_str, MODE_DIAG);
return g_string_free(modelist_str, false); /* 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) ...@@ -238,11 +238,15 @@ void control_select_usb_mode(void)
} }
if( usbmoded_get_diag_mode() ) { if( usbmoded_get_diag_mode() ) {
log_debug("Entering diagnostic mode!\n"); /* Assumption is that in diag-mode there is only
if( usbmoded_modelist ) { * one mode configured i.e. list head is diag-mode. */
/* XXX 1st entry is just assumed to be diag mode??? */ GList *iter = usbmoded_get_modelist();
GList *iter = usbmoded_modelist; if( !iter ) {
log_err("Diagnostic mode is not configured!");
}
else {
struct mode_list_elem *data = iter->data; struct mode_list_elem *data = iter->data;
log_debug("Entering diagnostic mode!");
control_set_usb_mode(data->mode_name); control_set_usb_mode(data->mode_name);
} }
goto EXIT; goto EXIT;
......
...@@ -481,7 +481,7 @@ worker_switch_to_mode(const char *mode) ...@@ -481,7 +481,7 @@ worker_switch_to_mode(const char *mode)
} }
/* go through all the dynamic modes if the modelist exists*/ /* 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; struct mode_list_elem *data = iter->data;
if( strcmp(mode, data->mode_name) ) if( strcmp(mode, data->mode_name) )
......
...@@ -104,31 +104,33 @@ ...@@ -104,31 +104,33 @@
/* -- usbmoded -- */ /* -- usbmoded -- */
bool usbmoded_get_rescue_mode (void); GList *usbmoded_get_modelist (void);
void usbmoded_set_rescue_mode (bool rescue_mode); void usbmoded_load_modelist (void);
bool usbmoded_get_diag_mode (void); void usbmoded_free_modelist (void);
void usbmoded_set_diag_mode (bool diag_mode); bool usbmoded_get_rescue_mode (void);
void usbmoded_set_cable_connection_delay(int delay_ms); void usbmoded_set_rescue_mode (bool rescue_mode);
int usbmoded_get_cable_connection_delay(void); bool usbmoded_get_diag_mode (void);
static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr); void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_allow_suspend (void); void usbmoded_set_cable_connection_delay(int delay_ms);
void usbmoded_delay_suspend (void); int usbmoded_get_cable_connection_delay(void);
bool usbmoded_init_done_p (void); static gboolean usbmoded_allow_suspend_timer_cb (gpointer aptr);
void usbmoded_set_init_done (bool reached); void usbmoded_allow_suspend (void);
void usbmoded_probe_init_done (void); void usbmoded_delay_suspend (void);
bool usbmoded_can_export (void); bool usbmoded_init_done_p (void);
void usbmoded_exit_mainloop (int exitcode); void usbmoded_set_init_done (bool reached);
void usbmoded_handle_signal (int signum); void usbmoded_probe_init_done (void);
static bool usbmoded_init (void); bool usbmoded_can_export (void);
static void usbmoded_cleanup (void); void usbmoded_exit_mainloop (int exitcode);
static void usbmoded_usage (void); void usbmoded_handle_signal (int signum);
static void usbmoded_parse_options (int argc, char *argv[]); 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 * Data
* ========================================================================= */ * ========================================================================= */
GList *usbmoded_modelist = 0;
static int usbmoded_exitcode = EXIT_FAILURE; static int usbmoded_exitcode = EXIT_FAILURE;
static GMainLoop *usbmoded_mainloop = NULL; static GMainLoop *usbmoded_mainloop = NULL;
...@@ -141,6 +143,37 @@ static bool usbmoded_systemd_notify = false; ...@@ -141,6 +143,37 @@ static bool usbmoded_systemd_notify = false;
* Functions * 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 * RESCUE_MODE
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
...@@ -398,9 +431,8 @@ void usbmoded_handle_signal(int signum) ...@@ -398,9 +431,8 @@ void usbmoded_handle_signal(int signum)
else if( signum == SIGHUP ) else if( signum == SIGHUP )
{ {
/* free and read in modelist again */ /* free and read in modelist again */
dynconfig_free_mode_list(usbmoded_modelist); usbmoded_free_modelist();
usbmoded_load_modelist();
usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode());
common_send_supported_modes_signal(); common_send_supported_modes_signal();
common_send_available_modes_signal(); common_send_available_modes_signal();
...@@ -478,7 +510,7 @@ static bool usbmoded_init(void) ...@@ -478,7 +510,7 @@ static bool usbmoded_init(void)
#endif #endif
/* always read dyn modes even if appsync is not used */ /* 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()) if(config_check_trigger())
trigger_init(); trigger_init();
...@@ -613,8 +645,8 @@ static void usbmoded_cleanup(void) ...@@ -613,8 +645,8 @@ static void usbmoded_cleanup(void)
/* Undo trigger_init() */ /* Undo trigger_init() */
trigger_stop(); trigger_stop();
/* Undo dynconfig_read_mode_list() */ /* Undo usbmoded_load_modelist() */
dynconfig_free_mode_list(usbmoded_modelist); usbmoded_free_modelist();
#ifdef APP_SYNC #ifdef APP_SYNC
/* Undo appsync_read_list() */ /* Undo appsync_read_list() */
......
...@@ -56,31 +56,28 @@ ...@@ -56,31 +56,28 @@
# define USB_MODED_SUSPEND_DELAY_MAXIMUM_MS \ # define USB_MODED_SUSPEND_DELAY_MAXIMUM_MS \
(USB_MODED_SUSPEND_DELAY_DEFAULT_MS * 2) (USB_MODED_SUSPEND_DELAY_DEFAULT_MS * 2)
/* ========================================================================= *
* Data
* ========================================================================= */
extern GList *usbmoded_modelist;
/* ========================================================================= * /* ========================================================================= *
* Functions * Functions
* ========================================================================= */ * ========================================================================= */
/* -- usbmoded -- */ /* -- usbmoded -- */
bool usbmoded_get_rescue_mode (void); GList *usbmoded_get_modelist (void);
void usbmoded_set_rescue_mode (bool rescue_mode); void usbmoded_load_modelist (void);
bool usbmoded_get_diag_mode (void); void usbmoded_free_modelist (void);
void usbmoded_set_diag_mode (bool diag_mode); bool usbmoded_get_rescue_mode (void);
void usbmoded_set_cable_connection_delay(int delay_ms); void usbmoded_set_rescue_mode (bool rescue_mode);
int usbmoded_get_cable_connection_delay(void); bool usbmoded_get_diag_mode (void);
void usbmoded_allow_suspend (void); void usbmoded_set_diag_mode (bool diag_mode);
void usbmoded_delay_suspend (void); void usbmoded_set_cable_connection_delay(int delay_ms);
bool usbmoded_init_done_p (void); int usbmoded_get_cable_connection_delay(void);
void usbmoded_set_init_done (bool reached); void usbmoded_allow_suspend (void);
void usbmoded_probe_init_done (void); void usbmoded_delay_suspend (void);
bool usbmoded_can_export (void); bool usbmoded_init_done_p (void);
void usbmoded_exit_mainloop (int exitcode); void usbmoded_set_init_done (bool reached);
void usbmoded_handle_signal (int signum); 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_ */ #endif /* USB_MODED_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment