diff --git a/Makefile.custom b/Makefile.custom index 8f3be87..783101f 100644 --- a/Makefile.custom +++ b/Makefile.custom @@ -316,10 +316,24 @@ endif # ---------------------------------------------------------------------------- .SUFFIXES: .q .p .g - -%.q : %.c ; $(CC) -o $@ -E $< $(CPPFLAGS) $(MCE_CFLAGS) -%.p : %.q ; cproto -s < $< | prettyproto.py > $@ -%.g : %.q ; cproto < $< | prettyproto.py > $@ +.PRECIOUS: .q + +PROTO_CPPFLAGS += -DAPP_SYNC +PROTO_CPPFLAGS += -DAPP_SYNC_DBUS +PROTO_CPPFLAGS += -DCONNMAN +PROTO_CPPFLAGS += -DDEAD_CODE +PROTO_CPPFLAGS += -DDEBIAN +PROTO_CPPFLAGS += -DMEEGOLOCK +PROTO_CPPFLAGS += -DOFONO +PROTO_CPPFLAGS += -DSYSTEMD +PROTO_CPPFLAGS += -DUSE_MER_SSU +PROTO_CPPFLAGS += -DCONNMAN_WORKS_BETTER=1 +PROTO_CPPFLAGS += -DVERBOSE_WAKELOCKING=1 + +%.q : CPPFLAGS += $(PROTO_CPPFLAGS) +%.q : %.c ; $(CC) -o $@ -E $< $(CPPFLAGS) -O +%.p : %.q prettyproto.groups ; cproto -s < $< | prettyproto.py > $@ +%.g : %.q prettyproto.groups ; cproto < $< | prettyproto.py > $@ clean:: $(RM) *.[qpg] src/*.[qpg] utils/*.[qpg] diff --git a/src/usb_moded-android.c b/src/usb_moded-android.c index 4ec4ded..5145dce 100644 --- a/src/usb_moded-android.c +++ b/src/usb_moded-android.c @@ -1,7 +1,7 @@ /** * @file usb_moded-android.c * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -34,11 +34,14 @@ #include /* ========================================================================= * - * Functions + * Prototypes * ========================================================================= */ -/* -- android -- */ +/* ------------------------------------------------------------------------- * + * ANDROID + * ------------------------------------------------------------------------- */ +static bool android_write_file (const char *path, const char *text); bool android_in_use (void); static bool android_probe (void); gchar *android_get_serial (void); diff --git a/src/usb_moded-android.h b/src/usb_moded-android.h index b0a7ecd..168ad6e 100644 --- a/src/usb_moded-android.h +++ b/src/usb_moded-android.h @@ -1,7 +1,7 @@ /** * @file usb_moded-android.h * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -44,7 +44,9 @@ * Prototypes * ========================================================================= */ -/* -- android -- */ +/* ------------------------------------------------------------------------- * + * ANDROID + * ------------------------------------------------------------------------- */ bool android_in_use (void); gchar *android_get_serial (void); diff --git a/src/usb_moded-appsync-dbus-private.h b/src/usb_moded-appsync-dbus-private.h index 1df7ef4..3b1e9b8 100644 --- a/src/usb_moded-appsync-dbus-private.h +++ b/src/usb_moded-appsync-dbus-private.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2018 Jolla Ltd. + * Copyright (C) 2018-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Philippe De Swert @@ -33,7 +33,9 @@ * Prototypes * ========================================================================= */ -/* -- dbusappsync -- */ +/* ------------------------------------------------------------------------- * + * DBUSAPPSYNC + * ------------------------------------------------------------------------- */ gboolean dbusappsync_init_connection(void); gboolean dbusappsync_init (void); diff --git a/src/usb_moded-appsync-dbus.c b/src/usb_moded-appsync-dbus.c index 9be0f6d..214a629 100644 --- a/src/usb_moded-appsync-dbus.c +++ b/src/usb_moded-appsync-dbus.c @@ -2,7 +2,7 @@ * @file usb_moded-dbus.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -38,7 +38,9 @@ * Prototypes * ========================================================================= */ -/* -- dbusappsync -- */ +/* ------------------------------------------------------------------------- * + * DBUSAPPSYNC + * ------------------------------------------------------------------------- */ static void dbusappsync_release_name (void); static gboolean dbusappsync_obtain_name (void); diff --git a/src/usb_moded-appsync.c b/src/usb_moded-appsync.c index 8715614..d4206a1 100644 --- a/src/usb_moded-appsync.c +++ b/src/usb_moded-appsync.c @@ -2,7 +2,7 @@ * @file usb_moded-appsync.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -40,27 +40,27 @@ * Prototypes * ========================================================================= */ -/* -- appsync -- */ - -static void appsync_free_elem (list_elem_t *elem); -static void appsync_free_elem_cb (gpointer elem, gpointer user_data); -void appsync_free_appsync_list (void); -static gint appsync_list_sort_func (gconstpointer a, gconstpointer b); -void appsync_read_list (int diag); -static list_elem_t *appsync_read_file (const gchar *filename, int diag); -int appsync_activate_sync (const char *mode); -int appsync_activate_sync_post (const char *mode); -int appsync_mark_active (const gchar *name, int post); - +/* ------------------------------------------------------------------------- * + * APPSYNC + * ------------------------------------------------------------------------- */ + +static void appsync_free_elem (list_elem_t *elem); +static void appsync_free_elem_cb (gpointer elem, gpointer user_data); +void appsync_free_appsync_list (void); +static gint appsync_list_sort_func (gconstpointer a, gconstpointer b); +void appsync_read_list (int diag); +static list_elem_t *appsync_read_file (const gchar *filename, int diag); +int appsync_activate_sync (const char *mode); +int appsync_activate_sync_post (const char *mode); +int appsync_mark_active (const gchar *name, int post); #ifdef APP_SYNC_DBUS -static gboolean appsync_enumerate_usb_cb (gpointer data); -static void appsync_start_enumerate_usb_timer (void); -static void appsync_cancel_enumerate_usb_timer(void); -static void appsync_enumerate_usb (void); -#endif - -void appsync_stop_apps (int post); -int appsync_stop (gboolean force); +static gboolean appsync_enumerate_usb_cb (gpointer data); +static void appsync_start_enumerate_usb_timer (void); +static void appsync_cancel_enumerate_usb_timer(void); +static void appsync_enumerate_usb (void); +#endif // APP_SYNC_DBUS +void appsync_stop_apps (int post); +int appsync_stop (gboolean force); /* ========================================================================= * * Data diff --git a/src/usb_moded-appsync.h b/src/usb_moded-appsync.h index 967199a..c2b7610 100644 --- a/src/usb_moded-appsync.h +++ b/src/usb_moded-appsync.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * author: Philippe De Swert * author: Philippe De Swert @@ -74,14 +74,16 @@ typedef struct list_elem_t * Prototypes * ========================================================================= */ -/* -- appsync -- */ +/* ------------------------------------------------------------------------- * + * APPSYNC + * ------------------------------------------------------------------------- */ void appsync_free_appsync_list (void); void appsync_read_list (int diag); int appsync_activate_sync (const char *mode); int appsync_activate_sync_post(const char *mode); int appsync_mark_active (const gchar *name, int post); -int appsync_stop (gboolean force); void appsync_stop_apps (int post); +int appsync_stop (gboolean force); #endif /* USB_MODED_APPSYNC_H_ */ diff --git a/src/usb_moded-common.c b/src/usb_moded-common.c index 414637c..f7e487f 100644 --- a/src/usb_moded-common.c +++ b/src/usb_moded-common.c @@ -35,11 +35,15 @@ typedef struct modemapping_t * Prototypes * ========================================================================= */ -/* -- cable -- */ +/* ------------------------------------------------------------------------- * + * CABLE_STATE + * ------------------------------------------------------------------------- */ const char *cable_state_repr(cable_state_t state); -/* -- common -- */ +/* ------------------------------------------------------------------------- * + * COMMON + * ------------------------------------------------------------------------- */ const char *common_map_mode_to_hardware (const char *internal_mode); const char *common_map_mode_to_external (const char *internal_mode); @@ -55,6 +59,7 @@ FILE *common_popen_ (const char *file, int line, co 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); static bool common_mode_in_list (const char *mode, char *const *modes); +bool common_modename_is_internal (const char *modename); int common_valid_mode (const char *mode); gchar *common_get_mode_list (mode_list_type_t type); @@ -439,6 +444,25 @@ static bool common_mode_in_list(const char *mode, char * const *modes) return false; } +/** Check if given usb mode is internal + * + * @param modename name of a more + * + * @return true if mode is internal, false otherwise + */ +bool +common_modename_is_internal(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)); +} + /** check if a given usb_mode exists * * @param mode The mode to look for @@ -457,24 +481,21 @@ int common_valid_mode(const char *mode) } else { - gchar *whitelist_value = 0; - gchar **whitelist_array = 0; + const modedata_t *data = usbmoded_get_modedata(mode); - if( (whitelist_value = config_get_mode_whitelist()) ) - whitelist_array = g_strsplit(whitelist_value, ",", 0); + if( data ) { + gchar *whitelist_value = 0; + gchar **whitelist_array = 0; - for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) { - mode_list_elem_t *data = iter->data; - if( strcmp(mode, data->mode_name) ) - continue; + if( (whitelist_value = config_get_mode_whitelist()) ) + whitelist_array = g_strsplit(whitelist_value, ",", 0); if (!whitelist_array || common_mode_in_list(data->mode_name, whitelist_array)) valid = 0; - break; - } - g_strfreev(whitelist_array); - g_free(whitelist_value); + g_strfreev(whitelist_array); + g_free(whitelist_value); + } } return valid; } @@ -521,7 +542,7 @@ gchar *common_get_mode_list(mode_list_type_t type) for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) { - mode_list_elem_t *data = iter->data; + modedata_t *data = iter->data; /* skip items in the hidden list */ if (common_mode_in_list(data->mode_name, hidden_modes_array)) diff --git a/src/usb_moded-common.h b/src/usb_moded-common.h index d8d06fe..74ea5f4 100644 --- a/src/usb_moded-common.h +++ b/src/usb_moded-common.h @@ -34,14 +34,18 @@ typedef enum waitres_t } waitres_t; /* ========================================================================= * - * Functions + * Prototypes * ========================================================================= */ -/* -- cable -- */ +/* ------------------------------------------------------------------------- * + * CABLE_STATE + * ------------------------------------------------------------------------- */ const char *cable_state_repr(cable_state_t state); -/* -- common -- */ +/* ------------------------------------------------------------------------- * + * COMMON + * ------------------------------------------------------------------------- */ const char *common_map_mode_to_hardware (const char *internal_mode); const char *common_map_mode_to_external (const char *internal_mode); @@ -55,6 +59,7 @@ int common_system_ (const char *file, int line, con 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); +bool common_modename_is_internal (const char *modename); int common_valid_mode (const char *mode); gchar *common_get_mode_list (mode_list_type_t type); diff --git a/src/usb_moded-config-private.h b/src/usb_moded-config-private.h index 643ae03..e3e8449 100644 --- a/src/usb_moded-config-private.h +++ b/src/usb_moded-config-private.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * Author: Philippe De Swert * Author: Philippe De Swert @@ -53,7 +53,9 @@ * Prototypes * ========================================================================= */ -/* -- config -- */ +/* ------------------------------------------------------------------------- * + * CONFIG + * ------------------------------------------------------------------------- */ char *config_find_mounts (void); int config_find_sync (void); @@ -65,8 +67,8 @@ char *config_get_trigger_subsystem (void); char *config_get_trigger_mode (void); char *config_get_trigger_property (void); char *config_get_trigger_value (void); +char *config_get_conf_string (const gchar *entry, const gchar *key); char *config_get_mode_setting (void); -int config_value_changed (GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value); set_config_result_t config_set_config_setting (const char *entry, const char *key, const char *value); set_config_result_t config_set_mode_setting (const char *mode); set_config_result_t config_set_hide_mode_setting (const char *mode); @@ -83,7 +85,6 @@ char *config_get_android_product_id (void); char *config_get_hidden_modes (void); char *config_get_mode_whitelist (void); int config_is_roaming_not_allowed (void); -char *config_get_conf_string (const gchar *entry, const gchar *key); /* ========================================================================= * * Macros diff --git a/src/usb_moded-config.c b/src/usb_moded-config.c index e1ba4aa..0326750 100644 --- a/src/usb_moded-config.c +++ b/src/usb_moded-config.c @@ -2,7 +2,7 @@ * @file usb_moded-config.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -57,7 +57,9 @@ * Prototypes * ========================================================================= */ -/* -- config -- */ +/* ------------------------------------------------------------------------- * + * CONFIG + * ------------------------------------------------------------------------- */ static int config_validate_ip (const char *ipadd); char *config_find_mounts (void); @@ -413,6 +415,7 @@ set_config_result_t config_set_config_setting(const char *entry, const char *key if( g_strcmp0(prev, value) ) { g_key_file_set_string(active_ini, entry, key, value); ret = SET_CONFIG_UPDATED; + umdbus_send_config_signal(entry, key, value); } /* Filter out dynamic data that matches static values */ @@ -434,7 +437,11 @@ set_config_result_t config_set_mode_setting(const char *mode) if (strcmp(mode, MODE_ASK) && common_valid_mode(mode)) return SET_CONFIG_ERROR; - return config_set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode); + + int ret = config_set_config_setting(MODE_SETTING_ENTRY, + MODE_SETTING_KEY, mode); + + return ret; } /* Builds the string used for hidden modes, when hide set to one builds the @@ -586,9 +593,7 @@ set_config_result_t config_set_mode_in_whitelist(const char *mode, int allowed) char *whitelist = config_make_modes_string(MODE_WHITELIST_KEY, mode, allowed); - if (whitelist) { - ret = config_set_mode_whitelist(whitelist); - } + ret = config_set_mode_whitelist(whitelist ?: ""); g_free(whitelist); @@ -615,55 +620,50 @@ set_config_result_t config_set_network_setting(const char *config, const char *s return SET_CONFIG_ERROR; } -char * config_get_network_setting(const char *config) +char *config_get_network_setting(const char *config) { LOG_REGISTER_CONTEXT; - char * ret = 0; - mode_list_elem_t *data; + char *ret = 0; - if(!strcmp(config, NETWORK_IP_KEY)) - { - ret = config_get_network_ip(); - if(!ret) - ret = strdup("192.168.2.15"); - } - else if(!strcmp(config, NETWORK_INTERFACE_KEY)) - { + modedata_t *data = 0; + if( !g_strcmp0(config, NETWORK_IP_KEY) ) { + if( !(ret = config_get_network_ip()) ) + ret = g_strdup("192.168.2.15"); + } + else if( !g_strcmp0(config, NETWORK_INTERFACE_KEY)) { /* check main configuration before using * the information from the specific mode */ - ret = config_get_network_interface(); + if( (ret = config_get_network_interface()) ) + goto EXIT; - if(ret) - goto end; /* no interface override specified, let's use the one * from the mode config */ - data = worker_get_usb_mode_data(); - if(data) - { - if(data->network_interface) - { - ret = strdup(data->network_interface); - goto end; - } + if( (data = worker_dup_usb_mode_data()) ) { + if( (ret = g_strdup(data->network_interface)) ) + goto EXIT; } - ret = strdup("usb0"); + + ret = g_strdup("usb0"); } - else if(!strcmp(config, NETWORK_GATEWAY_KEY)) - return config_get_network_gateway(); - else if(!strcmp(config, NETWORK_NETMASK_KEY)) - { - ret = config_get_network_netmask(); - if(!ret) - ret = strdup("255.255.255.0"); + else if( !g_strcmp0(config, NETWORK_GATEWAY_KEY) ) { + ret = config_get_network_gateway(); + } + else if( !g_strcmp0(config, NETWORK_NETMASK_KEY) ) { + if( !(ret = config_get_network_netmask()) ) + ret = g_strdup("255.255.255.0"); + } + else if( !g_strcmp0(config, NETWORK_NAT_INTERFACE_KEY) ) { + ret = config_get_network_nat_interface(); } - else if(!strcmp(config, NETWORK_NAT_INTERFACE_KEY)) - return config_get_network_nat_interface(); - else + else { /* no matching keys, return error */ - return NULL; -end: + } + +EXIT: + modedata_free(data); + return ret; } diff --git a/src/usb_moded-configfs.c b/src/usb_moded-configfs.c index 537237b..8abb66d 100644 --- a/src/usb_moded-configfs.c +++ b/src/usb_moded-configfs.c @@ -1,7 +1,7 @@ /** * @file usb_moded-configfs.c * - * Copyright (C) 2018 Jolla. All rights reserved. + * Copyright (C) 2018-2019 Jolla. All rights reserved. * * @author: Simo Piiroinen * @@ -64,7 +64,9 @@ * Prototypes * ========================================================================= */ -/* -- configfs -- */ +/* ------------------------------------------------------------------------- * + * CONFIGFS + * ------------------------------------------------------------------------- */ static gchar *configfs_get_conf (const char *key, const char *def); static void configfs_read_configuration (void); @@ -75,6 +77,9 @@ static const char *configfs_config_path (char *buff, size_t size, con static bool configfs_mkdir (const char *path); static bool configfs_rmdir (const char *path); static const char *configfs_register_function (const char *function); +#ifdef DEAD_CODE +static bool configfs_unregister_function (const char *function); +#endif //DEAD_CODE static const char *configfs_add_unit (const char *function, const char *unit); static bool configfs_remove_unit (const char *function, const char *unit); static bool configfs_enable_function (const char *function); @@ -86,6 +91,9 @@ static bool configfs_probe (void); static const char *configfs_udc_enable_value (void); static bool configfs_write_file (const char *path, const char *text); static bool configfs_read_file (const char *path, char *buff, size_t size); +#ifdef DEAD_CODE +static bool configfs_read_udc (char *buff, size_t size); +#endif // DEAD_CODE static bool configfs_write_udc (const char *text); bool configfs_set_udc (bool enable); bool configfs_init (void); diff --git a/src/usb_moded-configfs.h b/src/usb_moded-configfs.h index 2be0560..96d9dd2 100644 --- a/src/usb_moded-configfs.h +++ b/src/usb_moded-configfs.h @@ -1,7 +1,7 @@ /** * @file usb_moded-configfs.h * - * Copyright (C) 2018 Jolla. All rights reserved. + * Copyright (C) 2018-2019 Jolla. All rights reserved. * * @author: Simo Piiroinen * @@ -29,7 +29,9 @@ * Prototypes * ========================================================================= */ -/* -- configfs -- */ +/* ------------------------------------------------------------------------- * + * CONFIGFS + * ------------------------------------------------------------------------- */ bool configfs_in_use (void); bool configfs_set_udc (bool enable); diff --git a/src/usb_moded-control.c b/src/usb_moded-control.c index 4dab271..1f5d900 100644 --- a/src/usb_moded-control.c +++ b/src/usb_moded-control.c @@ -37,7 +37,9 @@ * Prototypes * ========================================================================= */ -/* -- usbmoded -- */ +/* ------------------------------------------------------------------------- * + * CONTROL + * ------------------------------------------------------------------------- */ void control_rethink_usb_charging_fallback(void); const char *control_get_external_mode (void); @@ -323,7 +325,7 @@ void control_select_usb_mode(void) log_err("Diagnostic mode is not configured!"); } else { - mode_list_elem_t *data = iter->data; + modedata_t *data = iter->data; log_debug("Entering diagnostic mode!"); control_set_usb_mode(data->mode_name); } diff --git a/src/usb_moded-control.h b/src/usb_moded-control.h index bc09f44..4e50ccf 100644 --- a/src/usb_moded-control.h +++ b/src/usb_moded-control.h @@ -27,7 +27,13 @@ # include "usb_moded.h" # include "usb_moded-common.h" -/* -- usbmoded -- */ +/* ========================================================================= * + * Prototypes + * ========================================================================= */ + +/* ------------------------------------------------------------------------- * + * CONTROL + * ------------------------------------------------------------------------- */ void control_rethink_usb_charging_fallback(void); const char *control_get_external_mode (void); @@ -37,7 +43,7 @@ void control_clear_target_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_mode_switched (const char *mode); void control_select_usb_mode (void); void control_set_cable_state (cable_state_t cable_state); cable_state_t control_get_cable_state (void); diff --git a/src/usb_moded-dbus-private.h b/src/usb_moded-dbus-private.h index dc6da43..0184083 100644 --- a/src/usb_moded-dbus-private.h +++ b/src/usb_moded-dbus-private.h @@ -53,8 +53,11 @@ typedef void (*usb_moded_get_name_owner_fn)(const char *owner); * Prototypes * ========================================================================= */ -/* -- umdbus -- */ +/* ------------------------------------------------------------------------- * + * UMDBUS + * ------------------------------------------------------------------------- */ +void umdbus_send_config_signal (const char *section, const char *key, const char *value); DBusConnection *umdbus_get_connection (void); gboolean umdbus_init_connection (void); gboolean umdbus_init_service (void); diff --git a/src/usb_moded-dbus.c b/src/usb_moded-dbus.c index d8c6d1a..9111608 100644 --- a/src/usb_moded-dbus.c +++ b/src/usb_moded-dbus.c @@ -55,9 +55,11 @@ * Prototypes * ========================================================================= */ -/* -- umdbus -- */ +/* ------------------------------------------------------------------------- * + * UMDBUS + * ------------------------------------------------------------------------- */ -static void umdbus_send_config_signal (const char *section, const char *key, const char *value); +void umdbus_send_config_signal (const char *section, const char *key, const char *value); static DBusHandlerResult umdbus_msg_handler (DBusConnection *const connection, DBusMessage *const msg, gpointer const user_data); DBusConnection *umdbus_get_connection (void); gboolean umdbus_init_connection (void); @@ -68,9 +70,9 @@ static DBusMessage *umdbus_new_signal (const char *signa static int umdbus_send_signal_ex (const char *signal_name, const char *content); static void umdbus_send_legacy_signal (const char *state_ind); void umdbus_send_current_state_signal (const char *state_ind); -static bool umsdbus_append_basic_entry (DBusMessageIter *iter, const char *key, int type, const void *val); -static bool umsdbus_append_int32_entry (DBusMessageIter *iter, const char *key, int val); -static bool umsdbus_append_string_entry (DBusMessageIter *iter, const char *key, const char *val); +static bool umdbus_append_basic_entry (DBusMessageIter *iter, const char *key, int type, const void *val); +static bool umdbus_append_int32_entry (DBusMessageIter *iter, const char *key, int val); +static bool umdbus_append_string_entry (DBusMessageIter *iter, const char *key, const char *val); static bool umdbus_append_mode_details (DBusMessage *msg, const char *mode_name); static void umdbus_send_mode_details_signal (const char *mode_name); void umdbus_send_target_state_signal (const char *state_ind); @@ -231,7 +233,7 @@ static const char umdbus_introspect_usbmoded[] = /** * Issues "sig_usb_config_ind" signal. */ -static void umdbus_send_config_signal(const char *section, const char *key, const char *value) +void umdbus_send_config_signal(const char *section, const char *key, const char *value) { LOG_REGISTER_CONTEXT; @@ -382,8 +384,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB { /* error checking is done when setting configuration */ int ret = config_set_mode_setting(config); - if (ret == SET_CONFIG_UPDATED) - umdbus_send_config_signal(MODE_SETTING_ENTRY, MODE_SETTING_KEY, config); if (SET_CONFIG_OK(ret)) { if((reply = dbus_message_new_method_return(msg))) @@ -405,8 +405,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB { /* error checking is done when setting configuration */ int ret = config_set_hide_mode_setting(config); - if (ret == SET_CONFIG_UPDATED) - umdbus_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_KEY, config); if (SET_CONFIG_OK(ret)) { if((reply = dbus_message_new_method_return(msg))) @@ -428,8 +426,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB { /* error checking is done when setting configuration */ int ret = config_set_unhide_mode_setting(config); - if (ret == SET_CONFIG_UPDATED) - umdbus_send_config_signal(MODE_SETTING_ENTRY, MODE_HIDE_KEY, config); if (SET_CONFIG_OK(ret)) { if((reply = dbus_message_new_method_return(msg))) @@ -460,8 +456,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB { /* error checking is done when setting configuration */ int ret = config_set_network_setting(config, setting); - if (ret == SET_CONFIG_UPDATED) - umdbus_send_config_signal(NETWORK_ENTRY, config, setting); if (SET_CONFIG_OK(ret)) { if((reply = dbus_message_new_method_return(msg))) @@ -547,8 +541,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB else { int ret = config_set_mode_whitelist(whitelist); - if (ret == SET_CONFIG_UPDATED) - umdbus_send_config_signal(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist); if (SET_CONFIG_OK(ret)) { if ((reply = dbus_message_new_method_return(msg))) @@ -570,14 +562,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB else { int ret = config_set_mode_in_whitelist(mode, enabled); - if (ret == SET_CONFIG_UPDATED) - { - char *whitelist = config_get_mode_whitelist(); - if (!whitelist) - whitelist = g_strdup(MODE_UNDEFINED); - umdbus_send_config_signal(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist); - g_free(whitelist); - } if (SET_CONFIG_OK(ret)) reply = dbus_message_new_method_return(msg); else @@ -933,7 +917,7 @@ void umdbus_send_current_state_signal(const char *state_ind) * @return true on success, false on failure */ static bool -umsdbus_append_basic_entry(DBusMessageIter *iter, const char *key, +umdbus_append_basic_entry(DBusMessageIter *iter, const char *key, int type, const void *val) { LOG_REGISTER_CONTEXT; @@ -993,12 +977,12 @@ umsdbus_append_basic_entry(DBusMessageIter *iter, const char *key, * @return true on success, false on failure */ static bool -umsdbus_append_int32_entry(DBusMessageIter *iter, const char *key, int val) +umdbus_append_int32_entry(DBusMessageIter *iter, const char *key, int val) { LOG_REGISTER_CONTEXT; dbus_int32_t arg = val; - return umsdbus_append_basic_entry(iter, key, DBUS_TYPE_INT32, &arg); + return umdbus_append_basic_entry(iter, key, DBUS_TYPE_INT32, &arg); } /** Append string key, variant:string value dict entry to dbus iterator @@ -1010,14 +994,14 @@ umsdbus_append_int32_entry(DBusMessageIter *iter, const char *key, int val) * @return true on success, false on failure */ static bool -umsdbus_append_string_entry(DBusMessageIter *iter, const char *key, +umdbus_append_string_entry(DBusMessageIter *iter, const char *key, const char *val) { LOG_REGISTER_CONTEXT; if( !val ) val = ""; - return umsdbus_append_basic_entry(iter, key, DBUS_TYPE_STRING, &val); + return umdbus_append_basic_entry(iter, key, DBUS_TYPE_STRING, &val); } /** Append dynamic mode configuration to dbus message @@ -1032,16 +1016,7 @@ umdbus_append_mode_details(DBusMessage *msg, const char *mode_name) { LOG_REGISTER_CONTEXT; - const mode_list_elem_t *data = 0; - - for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) - { - const mode_list_elem_t *iter_data = iter->data; - if( g_strcmp0(iter_data->mode_name, mode_name) ) - continue; - data = iter_data; - break; - } + const modedata_t *data = usbmoded_get_modedata(mode_name); DBusMessageIter body, dict; @@ -1059,7 +1034,7 @@ umdbus_append_mode_details(DBusMessage *msg, const char *mode_name) /* Note: mode_name is special case: It needs to be valid even * if the mode does not have dynamic configuration. */ - if( !umsdbus_append_string_entry(&dict, "mode_name", mode_name) ) + if( !umdbus_append_string_entry(&dict, "mode_name", mode_name) ) goto bailout_dict; /* For the rest of the mode attrs we use fallback data if there @@ -1067,10 +1042,10 @@ umdbus_append_mode_details(DBusMessage *msg, const char *mode_name) */ #define ADD_STR(name) \ - if( !umsdbus_append_string_entry(&dict, #name, data ? data->name : 0) )\ + if( !umdbus_append_string_entry(&dict, #name, data ? data->name : 0) )\ goto bailout_dict; #define ADD_INT(name) \ - if( !umsdbus_append_int32_entry(&dict, #name, data ? data->name : 0) )\ + if( !umdbus_append_int32_entry(&dict, #name, data ? data->name : 0) )\ goto bailout_dict; /* Attributes that we presume to be needed */ diff --git a/src/usb_moded-devicelock.c b/src/usb_moded-devicelock.c index a86ad97..1c87fef 100644 --- a/src/usb_moded-devicelock.c +++ b/src/usb_moded-devicelock.c @@ -2,7 +2,7 @@ * @file: usb_moded-devicelock.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -61,7 +61,9 @@ typedef enum devicelock_state_t * Prototypes * ========================================================================= */ -/* -- devicelock -- */ +/* ------------------------------------------------------------------------- * + * DEVICELOCK + * ------------------------------------------------------------------------- */ static const char *devicelock_state_repr (devicelock_state_t state); bool devicelock_have_export_permission(void); diff --git a/src/usb_moded-devicelock.h b/src/usb_moded-devicelock.h index a5daa70..a5c31ae 100644 --- a/src/usb_moded-devicelock.h +++ b/src/usb_moded-devicelock.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Vesa Halttunen @@ -57,10 +57,12 @@ * Prototypes * ========================================================================= */ -/* -- devicelock -- */ +/* ------------------------------------------------------------------------- * + * DEVICELOCK + * ------------------------------------------------------------------------- */ -bool devicelock_have_export_permission(void); -bool devicelock_start_listener (void); -void devicelock_stop_listener (void); +bool devicelock_have_export_permission(void); +bool devicelock_start_listener (void); +void devicelock_stop_listener (void); #endif /* USB_MODED_DEVICELOCK_H_ */ diff --git a/src/usb_moded-dsme.c b/src/usb_moded-dsme.c index 990be4f..49e50b1 100644 --- a/src/usb_moded-dsme.c +++ b/src/usb_moded-dsme.c @@ -1,7 +1,7 @@ /** * @file usb_moded-dsme.c * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Jonni Rainisto @@ -62,64 +62,59 @@ ",arg0='"DSME_DBUS_SERVICE"'" /* ========================================================================= * - * Functionality + * Prototypes * ========================================================================= */ /* ------------------------------------------------------------------------- * - * DSME_STATE_TRACKING + * DSME_STATE * ------------------------------------------------------------------------- */ -static const char *dsme_state_repr (dsme_state_t state); -static dsme_state_t dsme_state_parse (const char *name); - -static void dsme_state_update (dsme_state_t state); -static bool dsme_state_is_shutdown (void); -static bool dsme_state_is_user (void); +static const char *dsme_state_repr (dsme_state_t state); +static dsme_state_t dsme_state_parse (const char *name); +static void dsme_state_update (dsme_state_t state); +static bool dsme_state_is_shutdown(void); +static bool dsme_state_is_user (void); /* ------------------------------------------------------------------------- * - * DSME_SOCKET_IPC + * DSME_SOCKET * ------------------------------------------------------------------------- */ -static bool dsme_socket_send_message (void *msg); -static void dsme_socket_processwd_pong (void); -static void dsme_socket_processwd_init (void); -static void dsme_socket_processwd_quit (void); -static void dsme_socket_query_state (void); - -static gboolean dsme_socket_recv_cb (GIOChannel *source, GIOCondition condition, gpointer data); -static bool dsme_socket_is_connected (void); -static bool dsme_socket_connect (void); -static void dsme_socket_disconnect (void); +static bool dsme_socket_send_message (gpointer msg); +static void dsme_socket_processwd_pong(void); +static void dsme_socket_processwd_init(void); +static void dsme_socket_processwd_quit(void); +static void dsme_socket_query_state (void); +static gboolean dsme_socket_recv_cb (GIOChannel *source, GIOCondition condition, gpointer data); +static bool dsme_socket_is_connected (void); +static bool dsme_socket_connect (void); +static void dsme_socket_disconnect (void); /* ------------------------------------------------------------------------- * - * DSME_DBUS_IPC + * DSME_DBUS * ------------------------------------------------------------------------- */ -static void dsme_dbus_device_state_update (const char *state); -static void dsme_dbus_device_state_query_cb (DBusPendingCall *pending, void *aptr); -static void dsme_dbus_device_state_query (void); -static void dsme_dbus_device_state_cancel (void); -static void dsme_dbus_device_state_signal (DBusMessage *msg); - -static bool dsme_dbus_name_owner_available (void); -static void dsme_dbus_name_owner_update (const char *owner); -static void dsme_dbus_name_owner_query_cb (const char *owner); -static void dsme_dbus_name_owner_query (void); -static void dsme_dbus_name_owner_cancel (void); -static void dsme_dbus_name_owner_signal (DBusMessage *msg); - -static DBusHandlerResult dsme_dbus_filter_cb (DBusConnection *con, DBusMessage *msg, void *user_data); - -static bool dsme_dbus_init (void); -static void dsme_dbus_quit (void); +static void dsme_dbus_device_state_update (const char *state); +static void dsme_dbus_device_state_query_cb(DBusPendingCall *pending, void *aptr); +static void dsme_dbus_device_state_query (void); +static void dsme_dbus_device_state_cancel (void); +static void dsme_dbus_device_state_signal (DBusMessage *msg); +static bool dsme_dbus_name_owner_available (void); +static void dsme_dbus_name_owner_update (const char *owner); +static void dsme_dbus_name_owner_query_cb (const char *owner); +static void dsme_dbus_name_owner_query (void); +static void dsme_dbus_name_owner_cancel (void); +static void dsme_dbus_name_owner_signal (DBusMessage *msg); +static DBusHandlerResult dsme_dbus_filter_cb (DBusConnection *con, DBusMessage *msg, void *user_data); +static bool dsme_dbus_init (void); +static void dsme_dbus_quit (void); /* ------------------------------------------------------------------------- * - * MODULE_API + * DSME * ------------------------------------------------------------------------- */ -gboolean dsme_listener_start (void); -void dsme_listener_stop (void); -gboolean dsme_in_user_state (void); +gboolean dsme_start_listener(void); +void dsme_stop_listener(void); +gboolean dsme_in_user_state(void); /* ========================================================================= * * DSME_STATE_TRACKING @@ -858,7 +853,7 @@ dsme_dbus_quit(void) * ========================================================================= */ gboolean -dsme_listener_start(void) +dsme_start_listener(void) { LOG_REGISTER_CONTEXT; @@ -866,7 +861,7 @@ dsme_listener_start(void) } void -dsme_listener_stop(void) +dsme_stop_listener(void) { LOG_REGISTER_CONTEXT; diff --git a/src/usb_moded-dsme.h b/src/usb_moded-dsme.h index 11c3ff8..c7e6de1 100644 --- a/src/usb_moded-dsme.h +++ b/src/usb_moded-dsme.h @@ -1,7 +1,7 @@ /** * @file usb_moded-dsme.h * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -34,10 +34,12 @@ * Prototypes * ========================================================================= */ -/* -- dsme -- */ +/* ------------------------------------------------------------------------- * + * DSME + * ------------------------------------------------------------------------- */ -gboolean dsme_listener_start(void); -void dsme_listener_stop (void); -gboolean dsme_in_user_state (void); +gboolean dsme_start_listener(void); +void dsme_stop_listener(void); +gboolean dsme_in_user_state(void); #endif /* USB_MODED_DSME_H_ */ diff --git a/src/usb_moded-dyn-config.c b/src/usb_moded-dyn-config.c index e7d99cf..2059fba 100644 --- a/src/usb_moded-dyn-config.c +++ b/src/usb_moded-dyn-config.c @@ -2,7 +2,7 @@ * @file usb_moded-dyn-mode.c * * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -31,177 +31,216 @@ #include "usb_moded-log.h" #include +#include /* ========================================================================= * * Prototypes * ========================================================================= */ -/* -- dynconfig -- */ +/* ------------------------------------------------------------------------- * + * MODEDATA + * ------------------------------------------------------------------------- */ -void dynconfig_free_list_item(mode_list_elem_t *list_item); -void dynconfig_free_mode_list(GList *modelist); -static gint dynconfig_compare_modes (gconstpointer a, gconstpointer b); -GList *dynconfig_read_mode_list(int diag); -static mode_list_elem_t *dynconfig_read_mode_file(const gchar *filename); +static void modedata_free_cb(gpointer self); +void modedata_free (modedata_t *self); +modedata_t *modedata_copy (const modedata_t *that); +static gint modedata_sort_cb(gconstpointer a, gconstpointer b); +static modedata_t *modedata_load (const gchar *filename); + +/* ------------------------------------------------------------------------- * + * MODELIST + * ------------------------------------------------------------------------- */ + +void modelist_free(GList *modelist); +GList *modelist_load(bool diag); /* ========================================================================= * - * Functions + * MODEDATA * ========================================================================= */ -void dynconfig_free_list_item(mode_list_elem_t *list_item) +/** Type agnostice relase modedata_t object callback + * + * @param self Object pointer, or NULL + */ +static void +modedata_free_cb(gpointer self) +{ + modedata_free(self); +} + +/** Relase modedata_t object + * + * @param self Object pointer, or NULL + */ +void +modedata_free(modedata_t *self) { LOG_REGISTER_CONTEXT; - if( list_item ) { - free(list_item->mode_name); - free(list_item->mode_module); - free(list_item->network_interface); - free(list_item->sysfs_path); - free(list_item->sysfs_value); - free(list_item->sysfs_reset_value); - free(list_item->android_extra_sysfs_path); - free(list_item->android_extra_sysfs_value); - free(list_item->android_extra_sysfs_path2); - free(list_item->android_extra_sysfs_value2); - free(list_item->android_extra_sysfs_path3); - free(list_item->android_extra_sysfs_value3); - free(list_item->android_extra_sysfs_path4); - free(list_item->android_extra_sysfs_value4); - free(list_item->idProduct); - free(list_item->idVendorOverride); + if( self ) { + g_free(self->mode_name); + g_free(self->mode_module); + g_free(self->network_interface); + g_free(self->sysfs_path); + g_free(self->sysfs_value); + g_free(self->sysfs_reset_value); + g_free(self->android_extra_sysfs_path); + g_free(self->android_extra_sysfs_value); + g_free(self->android_extra_sysfs_path2); + g_free(self->android_extra_sysfs_value2); + g_free(self->android_extra_sysfs_path3); + g_free(self->android_extra_sysfs_value3); + g_free(self->android_extra_sysfs_path4); + g_free(self->android_extra_sysfs_value4); + g_free(self->idProduct); + g_free(self->idVendorOverride); #ifdef CONNMAN - free(list_item->connman_tethering); + g_free(self->connman_tethering); #endif - free(list_item); + free(self); } } -void dynconfig_free_mode_list(GList *modelist) +/** Clone modedata_t object + * + * @param that Object pointer, or NULL + * + * @return Object pointer, or NULL + */ +modedata_t * +modedata_copy(const modedata_t *that) { - LOG_REGISTER_CONTEXT; + modedata_t *self = 0; - if(modelist) - { - g_list_foreach(modelist, (GFunc) dynconfig_free_list_item, NULL); - g_list_free(modelist); - modelist = 0; - } -} + if( !that ) + goto EXIT; -static gint dynconfig_compare_modes(gconstpointer a, gconstpointer b) -{ - LOG_REGISTER_CONTEXT; + if( !(self = calloc(1, sizeof *self)) ) + goto EXIT; - mode_list_elem_t *aa = (mode_list_elem_t *)a; - mode_list_elem_t *bb = (mode_list_elem_t *)b; + self->mode_name = g_strdup(that->mode_name); + self->mode_module = g_strdup(that->mode_module); + self->appsync = that->appsync; + self->network = that->network; + self->mass_storage = that->mass_storage; + self->network_interface = g_strdup(that->network_interface); + self->sysfs_path = g_strdup(that->sysfs_path); + self->sysfs_value = g_strdup(that->sysfs_value); + self->sysfs_reset_value = g_strdup(that->sysfs_reset_value); + self->android_extra_sysfs_path = g_strdup(that->android_extra_sysfs_path); + self->android_extra_sysfs_value = g_strdup(that->android_extra_sysfs_value); + self->android_extra_sysfs_path2 = g_strdup(that->android_extra_sysfs_path2); + self->android_extra_sysfs_value2 = g_strdup(that->android_extra_sysfs_value2); + self->android_extra_sysfs_path3 = g_strdup(that->android_extra_sysfs_path3); + self->android_extra_sysfs_value3 = g_strdup(that->android_extra_sysfs_value3); + self->android_extra_sysfs_path4 = g_strdup(that->android_extra_sysfs_path4); + self->android_extra_sysfs_value4 = g_strdup(that->android_extra_sysfs_value4); + self->idProduct = g_strdup(that->idProduct); + self->idVendorOverride = g_strdup(that->idVendorOverride); + self->nat = that->nat; + self->dhcp_server = that->dhcp_server; +#ifdef CONNMAN + self->connman_tethering = g_strdup(that->connman_tethering); +#endif - return g_strcmp0(aa->mode_name, bb->mode_name); +EXIT: + return self; } -GList *dynconfig_read_mode_list(int diag) +/** Callback for sorting mode list alphabetically + * + * For use with g_list_sort() + * + * @param a Object pointer + * @param b Object pointer + * + * @return result of comparing object names + */ +static gint +modedata_sort_cb(gconstpointer a, gconstpointer b) { LOG_REGISTER_CONTEXT; - GDir *confdir; - GList *modelist = NULL; - const gchar *dirname; - mode_list_elem_t *list_item; - gchar *full_filename = NULL; - - if(diag) - confdir = g_dir_open(DIAG_DIR_PATH, 0, NULL); - else - confdir = g_dir_open(MODE_DIR_PATH, 0, NULL); - if(confdir) - { - while((dirname = g_dir_read_name(confdir)) != NULL) - { - log_debug("Read file %s\n", dirname); - if(diag) - full_filename = g_strconcat(DIAG_DIR_PATH, "/", dirname, NULL); - else - full_filename = g_strconcat(MODE_DIR_PATH, "/", dirname, NULL); - list_item = dynconfig_read_mode_file(full_filename); - /* free full_filename immediately as we do not use it anymore */ - free(full_filename); - if(list_item) - modelist = g_list_append(modelist, list_item); - } - g_dir_close(confdir); - } - else - log_debug("Mode confdir open failed or file is incomplete/invalid.\n"); + modedata_t *aa = (modedata_t *)a; + modedata_t *bb = (modedata_t *)b; - modelist = g_list_sort (modelist, dynconfig_compare_modes); - return modelist; + return g_strcmp0(aa->mode_name, bb->mode_name); } -static mode_list_elem_t *dynconfig_read_mode_file(const gchar *filename) +/** Load mode data from file + * + * @param filename Path to file from which to read + * + * @return Mode data object, or NULL + */ +static modedata_t * +modedata_load(const gchar *filename) { LOG_REGISTER_CONTEXT; - bool success = false; - GKeyFile *settingsfile = g_key_file_new(); - mode_list_elem_t *list_item = NULL; + modedata_t *self = NULL; + bool success = false; + GKeyFile *settingsfile = g_key_file_new(); if( !g_key_file_load_from_file(settingsfile, filename, G_KEY_FILE_NONE, NULL) ) { log_err("%s: can't read mode configuration file", filename); goto EXIT; } - list_item = calloc(1, sizeof *list_item); + if( !(self = calloc(1, sizeof *self)) ) + goto EXIT; // [MODE_ENTRY = "mode"] - list_item->mode_name = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_NAME_KEY, NULL); - list_item->mode_module = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_MODULE_KEY, NULL); + self->mode_name = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_NAME_KEY, NULL); + self->mode_module = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_MODULE_KEY, NULL); - log_debug("Dynamic mode name = %s\n", list_item->mode_name); - log_debug("Dynamic mode module = %s\n", list_item->mode_module); + log_debug("Dynamic mode name = %s\n", self->mode_name); + log_debug("Dynamic mode module = %s\n", self->mode_module); - list_item->appsync = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_NEEDS_APPSYNC_KEY, NULL); - list_item->mass_storage = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_MASS_STORAGE_KEY, NULL); - list_item->network = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_NETWORK_KEY, NULL); - list_item->network_interface = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_NETWORK_INTERFACE_KEY, NULL); + self->appsync = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_NEEDS_APPSYNC_KEY, NULL); + self->mass_storage = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_MASS_STORAGE_KEY, NULL); + self->network = g_key_file_get_integer(settingsfile, MODE_ENTRY, MODE_NETWORK_KEY, NULL); + self->network_interface = g_key_file_get_string(settingsfile, MODE_ENTRY, MODE_NETWORK_INTERFACE_KEY, NULL); // [MODE_OPTIONS_ENTRY = "options"] - list_item->sysfs_path = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_PATH, NULL); - list_item->sysfs_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_VALUE, NULL); - list_item->sysfs_reset_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_RESET_VALUE, NULL); - - list_item->android_extra_sysfs_path = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH, NULL); - list_item->android_extra_sysfs_path2 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH2, NULL); - list_item->android_extra_sysfs_path3 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH3, NULL); - list_item->android_extra_sysfs_path4 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH4, NULL); - list_item->android_extra_sysfs_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE, NULL); - list_item->android_extra_sysfs_value2 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE2, NULL); - list_item->android_extra_sysfs_value3 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE3, NULL); - list_item->android_extra_sysfs_value4 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE4, NULL); - - list_item->idProduct = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_IDPRODUCT, NULL); - list_item->idVendorOverride = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_IDVENDOROVERRIDE, NULL); - list_item->nat = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_NAT, NULL); - list_item->dhcp_server = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_DHCP_SERVER, NULL); + self->sysfs_path = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_PATH, NULL); + self->sysfs_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_VALUE, NULL); + self->sysfs_reset_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_SYSFS_RESET_VALUE, NULL); + + self->android_extra_sysfs_path = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH, NULL); + self->android_extra_sysfs_path2 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH2, NULL); + self->android_extra_sysfs_path3 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH3, NULL); + self->android_extra_sysfs_path4 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_PATH4, NULL); + self->android_extra_sysfs_value = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE, NULL); + self->android_extra_sysfs_value2 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE2, NULL); + self->android_extra_sysfs_value3 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE3, NULL); + self->android_extra_sysfs_value4 = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_ANDROID_EXTRA_SYSFS_VALUE4, NULL); + + self->idProduct = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_IDPRODUCT, NULL); + self->idVendorOverride = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_IDVENDOROVERRIDE, NULL); + self->nat = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_NAT, NULL); + self->dhcp_server = g_key_file_get_integer(settingsfile, MODE_OPTIONS_ENTRY, MODE_HAS_DHCP_SERVER, NULL); #ifdef CONNMAN - list_item->connman_tethering = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_CONNMAN_TETHERING, NULL); + self->connman_tethering = g_key_file_get_string(settingsfile, MODE_OPTIONS_ENTRY, MODE_CONNMAN_TETHERING, NULL); #endif - //log_debug("Dynamic mode sysfs path = %s\n", list_item->sysfs_path); - //log_debug("Dynamic mode sysfs value = %s\n", list_item->sysfs_value); - //log_debug("Android extra mode sysfs path2 = %s\n", list_item->android_extra_sysfs_path2); - //log_debug("Android extra value2 = %s\n", list_item->android_extra_sysfs_value2); + //log_debug("Dynamic mode sysfs path = %s\n", self->sysfs_path); + //log_debug("Dynamic mode sysfs value = %s\n", self->sysfs_value); + //log_debug("Android extra mode sysfs path2 = %s\n", self->android_extra_sysfs_path2); + //log_debug("Android extra value2 = %s\n", self->android_extra_sysfs_value2); - if( list_item->mode_name == NULL || list_item->mode_module == NULL ) { + if( self->mode_name == NULL || self->mode_module == NULL ) { log_err("%s: mode_name or mode_module not defined", filename); goto EXIT; } - if( list_item->network && list_item->network_interface == NULL) { + if( self->network && self->network_interface == NULL) { log_err("%s: network not fully defined", filename); goto EXIT; } - if( (list_item->sysfs_path && !list_item->sysfs_value) || - (list_item->sysfs_reset_value && !list_item->sysfs_path) ) { + if( (self->sysfs_path && !self->sysfs_value) || + (self->sysfs_reset_value && !self->sysfs_path) ) { /* In theory all of this is optional. * * In most cases 'sysfs_value' holds a list of functions to enable, @@ -223,7 +262,57 @@ static mode_list_elem_t *dynconfig_read_mode_file(const gchar *filename) g_key_file_free(settingsfile); if( !success ) - dynconfig_free_list_item(list_item), list_item = 0; + modedata_free(self), self = 0; + + return self; +} + +/* ========================================================================= * + * MODELIST + * ========================================================================= */ + +/** Release mode list + * + * @param modelist List pointer, or NULL + */ +void +modelist_free(GList *modelist) +{ + LOG_REGISTER_CONTEXT; + + g_list_free_full(modelist, modedata_free_cb); +} + +/** Load mode data files from configuration directory + * + * @param diag true to load diagnostic modes, or + * false for normal modes + * + * @return List of mode data objects, or NULL + */ +GList * +modelist_load(bool diag) +{ + LOG_REGISTER_CONTEXT; + + GList *modelist = 0; + const char *dirpath = diag ? DIAG_DIR_PATH : MODE_DIR_PATH; + gchar *pattern = g_strdup_printf("%s/*.ini", dirpath); + glob_t gb = {}; + + if( glob(pattern, 0, 0, &gb) != 0 ) + log_debug("no mode configuration ini-files found"); + + for( size_t i = 0; i < gb.gl_pathc; ++i ) { + const char *filepath = gb.gl_pathv[i]; + log_debug("Read file %s\n", filepath); + modedata_t *list_item = modedata_load(filepath); + if(list_item) + modelist = g_list_append(modelist, list_item); + } + + globfree(&gb); + g_free(pattern); - return list_item; + return g_list_sort(modelist, modedata_sort_cb); } diff --git a/src/usb_moded-dyn-config.h b/src/usb_moded-dyn-config.h index ba79f01..0ed0982 100644 --- a/src/usb_moded-dyn-config.h +++ b/src/usb_moded-dyn-config.h @@ -1,7 +1,7 @@ /* * * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * author: Philippe De Swert * author: Philippe De Swert @@ -30,6 +30,7 @@ #ifndef USB_MODED_DYN_CONFIG_H_ # define USB_MODED_DYN_CONFIG_H_ +# include # include /* ========================================================================= * @@ -93,42 +94,50 @@ /** * Struct keeping all the data needed for the definition of a dynamic mode */ -typedef struct mode_list_elem_t +typedef struct modedata_t { - char *mode_name; /**< Mode name */ - char *mode_module; /**< Needed module for given mode */ - int appsync; /**< Requires appsync or not */ - int network; /**< Bring up network or not */ - int mass_storage; /**< Use mass-storage functions */ - char *network_interface; /**< Which network interface to bring up if network needs to be enabled */ - char *sysfs_path; /**< Path to set sysfs options */ - char *sysfs_value; /**< Option name/value to write to sysfs */ - char *sysfs_reset_value; /**< Value to reset the the sysfs to default */ - char *android_extra_sysfs_path; /**< Path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value; /**< Static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path2; /**< Path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value2; /**< Static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path3; /**< Path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value3; /**< Static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_path4; /**< Path for static value that never changes that needs to be set by sysfs :( */ - char *android_extra_sysfs_value4; /**< Static value that never changes that needs to be set by sysfs :( */ - char *idProduct; /**< Product id to assign to a specific profile */ - char *idVendorOverride; /**< Temporary vendor override for special modes used by odms in testing/manufacturing */ - int nat; /**< If NAT should be set up in this mode or not */ - int dhcp_server; /**< if a DHCP server needs to be configured and started or not */ + gchar *mode_name; /**< Mode name */ + gchar *mode_module; /**< Needed module for given mode */ + int appsync; /**< Requires appsync or not */ + int network; /**< Bring up network or not */ + int mass_storage; /**< Use mass-storage functions */ + gchar *network_interface; /**< Which network interface to bring up if network needs to be enabled */ + gchar *sysfs_path; /**< Path to set sysfs options */ + gchar *sysfs_value; /**< Option name/value to write to sysfs */ + gchar *sysfs_reset_value; /**< Value to reset the the sysfs to default */ + gchar *android_extra_sysfs_path; /**< Path for static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_value; /**< Static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_path2; /**< Path for static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_value2; /**< Static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_path3; /**< Path for static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_value3; /**< Static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_path4; /**< Path for static value that never changes that needs to be set by sysfs :( */ + gchar *android_extra_sysfs_value4; /**< Static value that never changes that needs to be set by sysfs :( */ + gchar *idProduct; /**< Product id to assign to a specific profile */ + gchar *idVendorOverride; /**< Temporary vendor override for special modes used by odms in testing/manufacturing */ + int nat; /**< If NAT should be set up in this mode or not */ + int dhcp_server; /**< if a DHCP server needs to be configured and started or not */ # ifdef CONNMAN - char* connman_tethering; /**< Connman's tethering technology path */ + gchar *connman_tethering; /**< Connman's tethering technology path */ # endif -} mode_list_elem_t; +} modedata_t; /* ========================================================================= * * Prototypes * ========================================================================= */ -/* -- dynconfig -- */ +/* ------------------------------------------------------------------------- * + * MODEDATA + * ------------------------------------------------------------------------- */ -void dynconfig_free_list_item(mode_list_elem_t *list_item); -void dynconfig_free_mode_list(GList *modelist); -GList *dynconfig_read_mode_list(int diag); +void modedata_free(modedata_t *self); +modedata_t *modedata_copy(const modedata_t *that); + +/* ------------------------------------------------------------------------- * + * MODELIST + * ------------------------------------------------------------------------- */ + +void modelist_free(GList *modelist); +GList *modelist_load(bool diag); #endif /* USB_MODED_DYN_CONFIG_H_ */ diff --git a/src/usb_moded-log.c b/src/usb_moded-log.c index b64e3c3..948e6f5 100644 --- a/src/usb_moded-log.c +++ b/src/usb_moded-log.c @@ -2,7 +2,7 @@ * @file usb_moded-log.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2016-2018 Jolla Ltd. + * Copyright (C) 2016-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -39,7 +39,9 @@ * Prototypes * ========================================================================= */ -/* -- log -- */ +/* ------------------------------------------------------------------------- * + * LOG + * ------------------------------------------------------------------------- */ static char *log_strip (char *str); static void log_gettime (struct timeval *tv); diff --git a/src/usb_moded-log.h b/src/usb_moded-log.h index 79ad469..0eb4521 100644 --- a/src/usb_moded-log.h +++ b/src/usb_moded-log.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2016-2018 Jolla Ltd. + * Copyright (C) 2016-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Simo Piiroinen @@ -70,7 +70,9 @@ void context_leave(void *aptr); * Prototypes * ========================================================================= */ -/* -- log -- */ +/* ------------------------------------------------------------------------- * + * LOG + * ------------------------------------------------------------------------- */ void log_emit_va (const char *file, const char *func, int line, int lev, const char *fmt, va_list va); void log_emit_real (const char *file, const char *func, int line, int lev, const char *fmt, ...); diff --git a/src/usb_moded-mac.c b/src/usb_moded-mac.c index 2550051..7ea0830 100644 --- a/src/usb_moded-mac.c +++ b/src/usb_moded-mac.c @@ -1,7 +1,7 @@ /** * @file usb_moded-mac.c * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -33,7 +33,9 @@ * Prototypes * ========================================================================= */ -/* -- mac -- */ +/* ------------------------------------------------------------------------- * + * MAC + * ------------------------------------------------------------------------- */ static void mac_random_ether_addr (unsigned char *addr); void mac_generate_random_mac(void); diff --git a/src/usb_moded-mac.h b/src/usb_moded-mac.h index 6cb9d78..626c123 100644 --- a/src/usb_moded-mac.h +++ b/src/usb_moded-mac.h @@ -1,7 +1,7 @@ /** * @file usb_moded-mac.h * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -28,7 +28,9 @@ * Prototypes * ========================================================================= */ -/* -- mac -- */ +/* ------------------------------------------------------------------------- * + * MAC + * ------------------------------------------------------------------------- */ void mac_generate_random_mac(void); char *mac_read_mac (void); diff --git a/src/usb_moded-modes.h b/src/usb_moded-modes.h index 95517a7..0eba032 100644 --- a/src/usb_moded-modes.h +++ b/src/usb_moded-modes.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Philippe De Swert @@ -32,12 +32,57 @@ * Constants * ========================================================================= */ -/* possible values for the mode - * the first two are internal only. - */ +/* ------------------------------------------------------------------------- * + * Internal modes + * + * These modes are defined internally within usb-moded and are thus + * always available. + * + * Generally speaking these are also activated automatically and thus + * not really selectable - except: + * - MODE_ASK which can be set as default mode + * - MODE_CHARGING which can be acticated on request too + * ------------------------------------------------------------------------- */ +/** No cable connected */ # define MODE_UNDEFINED "undefined" + +/** Pending mode activation + * + * Used for signaling "in between modes" state. + */ +# define MODE_BUSY "busy" + +/** Connected to a dedicated charger */ +# define MODE_CHARGER "dedicated_charger" + +/** Blocked mode selection + * + * While prerequisites for dynamic mode activation are not met e.g. + * device is locked, pc connection is used for charging. + */ +# define MODE_CHARGING_FALLBACK "charging_only_fallback" + +/** Pending mode selection + * + * While mode selection dialog is shown to user, pc connection + * is used for charging. + */ # define MODE_ASK "ask" + +/** Charging only selected */ +# define MODE_CHARGING "charging_only" + +/* ------------------------------------------------------------------------- * + * Dynamic modes + * + * These modes are defined in usb-moded configuration files. + * + * From usb-moded point of view mode names have no special meaning, + * but a set of known values is still defined (and are likely to + * have localized name presentation in UI context). + * ------------------------------------------------------------------------- */ + # define MODE_MASS_STORAGE "mass_storage" # define MODE_DEVELOPER "developer_mode" # define MODE_MTP "mtp_mode" @@ -46,18 +91,5 @@ # define MODE_DIAG "diag_mode" # define MODE_ADB "adb_mode" # define MODE_PC_SUITE "pc_suite" -# define MODE_CHARGING "charging_only" -# define MODE_BUSY "busy" - -/** - * - * MODE_CHARGING : user manually selected charging mode - * MODE_CHARGING_FALLBACK : mode selection is not done by the user so we fallback to a charging mode to get some power - * MODE_CHARGER : there is a dedicated charger connected to the USB port - * - * the two last ones cannot be set and are not full modes - **/ -# define MODE_CHARGING_FALLBACK "charging_only_fallback" -# define MODE_CHARGER "dedicated_charger" #endif /* USB_MODED_MODES_H_ */ diff --git a/src/usb_moded-modesetting.c b/src/usb_moded-modesetting.c index 6c4d639..bf88df7 100644 --- a/src/usb_moded-modesetting.c +++ b/src/usb_moded-modesetting.c @@ -48,29 +48,44 @@ #include #include #include +#include /* ========================================================================= * - * Prototypes + * Types * ========================================================================= */ -/* -- modesetting -- */ - -static void modesetting_track_value (const char *path, const char *text); -void modesetting_verify_values (void); - -static char *modesetting_strip (char *str); -static char *modesetting_read_from_file (const char *path, size_t maxsize); -int modesetting_write_to_file_real (const char *file, int line, const char *func, const char *path, const char *text); - -static bool modesetting_enter_mass_storage_mode (mode_list_elem_t *data); -static int modesetting_leave_mass_storage_mode (mode_list_elem_t *data); -static void modesetting_report_mass_storage_blocker(const char *mountpoint, int try); +typedef struct storage_info_t +{ + gchar *si_mountpoint; + gchar *si_mountdevice;; +} storage_info_t; -bool modesetting_enter_dynamic_mode (void); -void modesetting_leave_dynamic_mode (void); +/* ========================================================================= * + * Prototypes + * ========================================================================= */ -void modesetting_init (void); -void modesetting_quit (void); +/* ------------------------------------------------------------------------- * + * MODESETTING + * ------------------------------------------------------------------------- */ + +static void modesetting_track_value (const char *path, const char *text); +void modesetting_verify_values (void); +static char *modesetting_strip (char *str); +static char *modesetting_read_from_file (const char *path, size_t maxsize); +int modesetting_write_to_file_real (const char *file, int line, const char *func, const char *path, const char *text); +bool modesetting_is_mounted (const char *mountpoint); +bool modesetting_mount (const char *mountpoint); +bool modesetting_unmount (const char *mountpoint); +static gchar *modesetting_mountdev (const char *mountpoint); +static void modesetting_free_storage_info (storage_info_t *info); +static storage_info_t *modesetting_get_storage_info (size_t *pcount); +static bool modesetting_enter_mass_storage_mode (const modedata_t *data); +static int modesetting_leave_mass_storage_mode (const modedata_t *data); +static void modesetting_report_mass_storage_blocker(const char *mountpoint, int try); +bool modesetting_enter_dynamic_mode (void); +void modesetting_leave_dynamic_mode (void); +void modesetting_init (void); +void modesetting_quit (void); /* ========================================================================= * * Data @@ -280,22 +295,6 @@ int modesetting_write_to_file_real(const char *file, int line, const char *func, return err; } -#include - -typedef struct storage_info_t -{ - gchar *si_mountpoint; - gchar *si_mountdevice;; -} storage_info_t; - -static void modesetting_free_storage_info(storage_info_t *info); -static storage_info_t * modesetting_get_storage_info(size_t *pcount); - -bool modesetting_is_mounted(const char *mountpoint); -bool modesetting_mount(const char *mountpoint); -bool modesetting_unmount(const char *mountpoint); -char *modesetting_mountdev(const char *mountpoint); - bool modesetting_is_mounted(const char *mountpoint) { LOG_REGISTER_CONTEXT; @@ -323,7 +322,7 @@ bool modesetting_unmount(const char *mountpoint) return common_system(cmd) == 0; } -gchar *modesetting_mountdev(const char *mountpoint) +static gchar *modesetting_mountdev(const char *mountpoint) { LOG_REGISTER_CONTEXT; @@ -427,7 +426,7 @@ modesetting_get_storage_info(size_t *pcount) return *pcount = count, info; } -static bool modesetting_enter_mass_storage_mode(mode_list_elem_t *data) +static bool modesetting_enter_mass_storage_mode(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -565,7 +564,7 @@ static bool modesetting_enter_mass_storage_mode(mode_list_elem_t *data) return ack; } -static int modesetting_leave_mass_storage_mode(mode_list_elem_t *data) +static int modesetting_leave_mass_storage_mode(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -705,7 +704,7 @@ bool modesetting_enter_dynamic_mode(void) bool ack = false; - mode_list_elem_t *data; + const modedata_t *data; log_debug("DYNAMIC MODE: SETUP"); @@ -857,9 +856,7 @@ void modesetting_leave_dynamic_mode(void) log_debug("DYNAMIC MODE: CLEANUP"); - mode_list_elem_t *data; - - data = worker_get_usb_mode_data(); + const modedata_t *data = worker_get_usb_mode_data(); /* - - - - - - - - - - - - - - - - - - - * * Is a dynamic mode? diff --git a/src/usb_moded-modesetting.h b/src/usb_moded-modesetting.h index 4ae9c46..7c3a997 100644 --- a/src/usb_moded-modesetting.h +++ b/src/usb_moded-modesetting.h @@ -2,7 +2,7 @@ * @file usb_moded-modesetting.h * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -35,10 +35,15 @@ * Prototypes * ========================================================================= */ -/* -- modesetting -- */ +/* ------------------------------------------------------------------------- * + * MODESETTING + * ------------------------------------------------------------------------- */ void modesetting_verify_values (void); int modesetting_write_to_file_real(const char *file, int line, const char *func, const char *path, const char *text); +bool modesetting_is_mounted (const char *mountpoint); +bool modesetting_mount (const char *mountpoint); +bool modesetting_unmount (const char *mountpoint); bool modesetting_enter_dynamic_mode(void); void modesetting_leave_dynamic_mode(void); void modesetting_init (void); diff --git a/src/usb_moded-modules.c b/src/usb_moded-modules.c index 156acef..a4df19d 100644 --- a/src/usb_moded-modules.c +++ b/src/usb_moded-modules.c @@ -2,7 +2,7 @@ * @file usb_moded-modules.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -42,13 +42,17 @@ * Prototypes * ========================================================================= */ -/* -- modules -- */ - -bool modules_init (void); -bool modules_in_use (void); -void modules_quit (void); -int modules_load_module (const char *module); -int modules_unload_module (const char *module); +/* ------------------------------------------------------------------------- * + * MODULES + * ------------------------------------------------------------------------- */ + +static bool modules_have_module (const char *module); +bool modules_in_use (void); +static bool modules_probe (void); +bool modules_init (void); +void modules_quit (void); +int modules_load_module (const char *module); +int modules_unload_module(const char *module); /* ========================================================================= * * Data diff --git a/src/usb_moded-modules.h b/src/usb_moded-modules.h index b53da16..b375e51 100644 --- a/src/usb_moded-modules.h +++ b/src/usb_moded-modules.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Philippe De Swert @@ -46,12 +46,14 @@ * Prototypes * ========================================================================= */ -/* -- modules -- */ +/* ------------------------------------------------------------------------- * + * MODULES + * ------------------------------------------------------------------------- */ -bool modules_in_use (void); -bool modules_init (void); -void modules_quit (void); -int modules_load_module (const char *module); -int modules_unload_module (const char *module); +bool modules_in_use (void); +bool modules_init (void); +void modules_quit (void); +int modules_load_module (const char *module); +int modules_unload_module(const char *module); #endif /* USB_MODED_MODULES_H_ */ diff --git a/src/usb_moded-network.c b/src/usb_moded-network.c index cffb6f8..b686f2a 100644 --- a/src/usb_moded-network.c +++ b/src/usb_moded-network.c @@ -2,7 +2,7 @@ * @file usb-moded_network.c : (De)activates network depending on the network setting system. * * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -73,23 +73,38 @@ typedef struct ipforward_data_t * Prototypes * ========================================================================= */ -/* -- network -- */ +/* ------------------------------------------------------------------------- * + * NETWORK + * ------------------------------------------------------------------------- */ static void network_free_ipforward_data (ipforward_data_t *ipforward); static int network_check_interface (char *interface); -static char *network_get_interface (mode_list_elem_t *data); -static int network_set_usb_ip_forward (mode_list_elem_t *data, ipforward_data_t *ipforward); +static char *network_get_interface (const modedata_t *data); +static int network_set_usb_ip_forward (const modedata_t *data, ipforward_data_t *ipforward); static void network_clean_usb_ip_forward(void); static int network_checklink (void); -static int network_write_udhcpd_conf (ipforward_data_t *ipforward, mode_list_elem_t *data); -int network_set_up_dhcpd (mode_list_elem_t *data); -int network_up (mode_list_elem_t *data); -int network_down (mode_list_elem_t *data); +static int network_write_udhcpd_conf (ipforward_data_t *ipforward, const modedata_t *data); +int network_set_up_dhcpd (const modedata_t *data); +int network_up (const modedata_t *data); +int network_down (const modedata_t *data); int network_update (void); -/* -- connman -- */ +/* ------------------------------------------------------------------------- * + * UTILITY + * ------------------------------------------------------------------------- */ + +#ifdef OFONO +static int get_roaming (void); +#endif // OFONO + +#if CONNMAN_WORKS_BETTER +static int append_variant(DBusMessageIter *iter, const char *property, int type, const char *value); +#endif // CONNMAN_WORKS_BETTER + +/* ------------------------------------------------------------------------- * + * CONNMAN + * ------------------------------------------------------------------------- */ -#ifdef CONNMAN static gboolean connman_try_set_tethering (DBusConnection *connection, const char *path, gboolean on); gboolean connman_set_tethering (const char *path, gboolean on); static char *connman_parse_manager_reply (DBusMessage *reply, const char *req_service); @@ -98,7 +113,6 @@ static int connman_set_cellular_online (DBusConnection *dbus_conn_connman, static int connman_wifi_power_control (DBusConnection *dbus_conn_connman, int on); static int connman_get_connection_data (ipforward_data_t *ipforward); static int connman_reset_state (void); -#endif /* ========================================================================= * * Data @@ -143,7 +157,7 @@ static int network_check_interface(char *interface) return ret; } -static char* network_get_interface(mode_list_elem_t *data) +static char *network_get_interface(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -180,7 +194,7 @@ static char* network_get_interface(mode_list_elem_t *data) * Turn on ip forwarding on the usb interface * @return: 0 on success, 1 on failure */ -static int network_set_usb_ip_forward(mode_list_elem_t *data, ipforward_data_t *ipforward) +static int network_set_usb_ip_forward(const modedata_t *data, ipforward_data_t *ipforward) { LOG_REGISTER_CONTEXT; @@ -375,7 +389,7 @@ static int network_checklink(void) * Write udhcpd.conf * @ipforward : NULL if we want a simple config, otherwise include dns info etc... */ -static int network_write_udhcpd_conf(ipforward_data_t *ipforward, mode_list_elem_t *data) +static int network_write_udhcpd_conf(ipforward_data_t *ipforward, const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -950,7 +964,7 @@ static int connman_reset_state(void) /** * Write out /etc/udhcpd.conf conf so the config is available when it gets started */ -int network_set_up_dhcpd(mode_list_elem_t *data) +int network_set_up_dhcpd(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -1034,7 +1048,7 @@ static int append_variant(DBusMessageIter *iter, const char *property, * Activate the network interface * */ -int network_up(mode_list_elem_t *data) +int network_up(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -1202,7 +1216,7 @@ int network_up(mode_list_elem_t *data) * Deactivate the network interface * */ -int network_down(mode_list_elem_t *data) +int network_down(const modedata_t *data) { LOG_REGISTER_CONTEXT; @@ -1281,11 +1295,12 @@ int network_update(void) LOG_REGISTER_CONTEXT; if( control_get_cable_state() == CABLE_STATE_PC_CONNECTED ) { - mode_list_elem_t *data = worker_get_usb_mode_data(); + modedata_t *data = worker_dup_usb_mode_data(); if( data && data->network ) { network_down(data); network_up(data); } + modedata_free(data); } return 0; } diff --git a/src/usb_moded-network.h b/src/usb_moded-network.h index 10166ce..728008e 100644 --- a/src/usb_moded-network.h +++ b/src/usb_moded-network.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2013-2018 Jolla Ltd. + * Copyright (C) 2013-2019 Jolla Ltd. * * Author: Philippe De Swert * Author: Philippe De Swert @@ -33,17 +33,21 @@ * Prototypes * ========================================================================= */ -/* -- connman -- */ +/* ------------------------------------------------------------------------- * + * CONNMAN + * ------------------------------------------------------------------------- */ # ifdef CONNMAN gboolean connman_set_tethering(const char *path, gboolean on); # endif -/* -- network -- */ +/* ------------------------------------------------------------------------- * + * NETWORK + * ------------------------------------------------------------------------- */ -int network_set_up_dhcpd(mode_list_elem_t *data); -int network_up (mode_list_elem_t *data); -int network_down (mode_list_elem_t *data); +int network_set_up_dhcpd(const modedata_t *data); +int network_up (const modedata_t *data); +int network_down (const modedata_t *data); int network_update (void); #endif /* USB_MODED_NETWORK_H_ */ diff --git a/src/usb_moded-sigpipe.c b/src/usb_moded-sigpipe.c index c464c7d..9a6d41f 100644 --- a/src/usb_moded-sigpipe.c +++ b/src/usb_moded-sigpipe.c @@ -2,7 +2,7 @@ * @file usb_moded-sigpipe.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the Lesser GNU General Public License @@ -33,7 +33,9 @@ * Prototypes * ========================================================================= */ -/* -- sigpipe -- */ +/* ------------------------------------------------------------------------- * + * SIGPIPE + * ------------------------------------------------------------------------- */ static gboolean sigpipe_read_signal_cb(GIOChannel *channel, GIOCondition condition, gpointer data); static void sigpipe_trap_signal_cb(int sig); diff --git a/src/usb_moded-sigpipe.h b/src/usb_moded-sigpipe.h index e16488b..84a570d 100644 --- a/src/usb_moded-sigpipe.h +++ b/src/usb_moded-sigpipe.h @@ -2,7 +2,7 @@ * @file usb_moded-sigpipe.c * * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the Lesser GNU General Public License @@ -24,7 +24,13 @@ #include -/* -- sigpipe -- */ +/* ========================================================================= * + * Prototypes + * ========================================================================= */ + +/* ------------------------------------------------------------------------- * + * SIGPIPE + * ------------------------------------------------------------------------- */ bool sigpipe_init(void); diff --git a/src/usb_moded-ssu.c b/src/usb_moded-ssu.c index 4f1ca7e..731447e 100644 --- a/src/usb_moded-ssu.c +++ b/src/usb_moded-ssu.c @@ -1,7 +1,7 @@ /** * @file usb_moded-ssu.c * - * Copyright (C) 2016-2018 Jolla. All rights reserved. + * Copyright (C) 2016-2019 Jolla. All rights reserved. * * @author: Simo Piiroinen * @@ -33,7 +33,9 @@ * Prototypes * ========================================================================= */ -/* -- ssu -- */ +/* ------------------------------------------------------------------------- * + * SSU + * ------------------------------------------------------------------------- */ static void ssu_free_handle (void); static ssusysinfo_t *ssu_get_handle (void); diff --git a/src/usb_moded-ssu.h b/src/usb_moded-ssu.h index 81ab749..827d7af 100644 --- a/src/usb_moded-ssu.h +++ b/src/usb_moded-ssu.h @@ -1,7 +1,7 @@ /** * @file usb_moded-ssu.h * - * Copyright (C) 2016-2018 Jolla. All rights reserved. + * Copyright (C) 2016-2019 Jolla. All rights reserved. * * @author: Simo Piiroinen * @@ -29,7 +29,9 @@ * Prototypes * ========================================================================= */ -/* -- ssu -- */ +/* ------------------------------------------------------------------------- * + * SSU + * ------------------------------------------------------------------------- */ gchar *ssu_get_manufacturer_name(void); gchar *ssu_get_product_name (void); diff --git a/src/usb_moded-systemd.c b/src/usb_moded-systemd.c index 8e87a42..f411c36 100644 --- a/src/usb_moded-systemd.c +++ b/src/usb_moded-systemd.c @@ -1,7 +1,7 @@ /** * @file usb_moded-systemd.c * - * Copyright (C) 2013-2018 Jolla oy. All rights reserved. + * Copyright (C) 2013-2019 Jolla oy. All rights reserved. * * @author: Philippe De Swert * @author: Slava Monich @@ -39,7 +39,9 @@ * Prototypes * ========================================================================= */ -/* -- systemd -- */ +/* ------------------------------------------------------------------------- * + * SYSTEMD + * ------------------------------------------------------------------------- */ gboolean systemd_control_service(const char *name, const char *method); gboolean systemd_control_start (void); diff --git a/src/usb_moded-systemd.h b/src/usb_moded-systemd.h index d8c2a7e..faf80e0 100644 --- a/src/usb_moded-systemd.h +++ b/src/usb_moded-systemd.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2018 Jolla Oy. All rights reserved. + * Copyright (C) 2013-2019 Jolla Oy. All rights reserved. * * author: Philippe De Swert * author: Simo Piiroinen @@ -35,7 +35,9 @@ * Prototypes * ========================================================================= */ -/* -- systemd -- */ +/* ------------------------------------------------------------------------- * + * SYSTEMD + * ------------------------------------------------------------------------- */ gboolean systemd_control_service(const char *name, const char *method); gboolean systemd_control_start (void); diff --git a/src/usb_moded-trigger.c b/src/usb_moded-trigger.c index 7e0ef42..ce8da4c 100644 --- a/src/usb_moded-trigger.c +++ b/src/usb_moded-trigger.c @@ -2,7 +2,7 @@ * @file usb_moded-trigger.c * * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2014-2018 Jolla Ltd. + * Copyright (C) 2014-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -40,13 +40,15 @@ * Prototypes * ========================================================================= */ -/* -- trigger -- */ +/* ------------------------------------------------------------------------- * + * TRIGGER + * ------------------------------------------------------------------------- */ static void trigger_udev_error_cb (gpointer data); -static gboolean trigger_udev_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data); -static void trigger_parse_udev_properties(struct udev_device *dev); bool trigger_init (void); +static gboolean trigger_udev_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data); void trigger_stop (void); +static void trigger_parse_udev_properties(struct udev_device *dev); /* ========================================================================= * * Data diff --git a/src/usb_moded-trigger.h b/src/usb_moded-trigger.h index 6a15980..d8c53ad 100644 --- a/src/usb_moded-trigger.h +++ b/src/usb_moded-trigger.h @@ -2,7 +2,7 @@ * @file usb_moded-trigger.h * * Copyright (C) 2011 Nokia Corporation. All rights reserved. - * Copyright (C) 2018 Jolla Ltd. + * Copyright (C) 2018-2019 Jolla Ltd. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -31,9 +31,11 @@ * Prototypes * ========================================================================= */ -/* -- trigger -- */ +/* ------------------------------------------------------------------------- * + * TRIGGER + * ------------------------------------------------------------------------- */ -bool trigger_init(void); -void trigger_stop(void); +bool trigger_init(void); +void trigger_stop(void); #endif /* USB_MODED_TRIGGER_H_ */ diff --git a/src/usb_moded-udev.c b/src/usb_moded-udev.c index d66fb48..8e76e21 100644 --- a/src/usb_moded-udev.c +++ b/src/usb_moded-udev.c @@ -42,7 +42,9 @@ * Prototypes * ========================================================================= */ -/* -- umudev -- */ +/* ------------------------------------------------------------------------- * + * UMUDEV + * ------------------------------------------------------------------------- */ static gboolean umudev_cable_state_timer_cb (gpointer aptr); static void umudev_cable_state_stop_timer (void); @@ -51,14 +53,13 @@ static bool umudev_cable_state_connected (void); static cable_state_t umudev_cable_state_get (void); static void umudev_cable_state_set (cable_state_t state); static void umudev_cable_state_changed (void); -static void umudev_cable_state_from_udev (cable_state_t state); - -static void umudev_io_error_cb (gpointer data); -static gboolean umudev_io_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data); -static void umudev_parse_properties (struct udev_device *dev, bool initial); -static int umudev_score_as_power_supply(const char *syspath); -gboolean umudev_init (void); -void umudev_quit (void); +static void umudev_cable_state_from_udev (cable_state_t curr); +static void umudev_io_error_cb (gpointer data); +static gboolean umudev_io_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data); +static void umudev_parse_properties (struct udev_device *dev, bool initial); +static int umudev_score_as_power_supply (const char *syspath); +gboolean umudev_init (void); +void umudev_quit (void); /* ========================================================================= * * Data diff --git a/src/usb_moded-udev.h b/src/usb_moded-udev.h index 8039fd1..3ccd814 100644 --- a/src/usb_moded-udev.h +++ b/src/usb_moded-udev.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2018 Jolla Ltd. + * Copyright (C) 2018-2019 Jolla Ltd. * * Author: Philippe De Swert * @author: Simo Piiroinen @@ -36,7 +36,9 @@ * Prototypes * ========================================================================= */ -/* -- umudev -- */ +/* ------------------------------------------------------------------------- * + * UMUDEV + * ------------------------------------------------------------------------- */ gboolean umudev_init(void); void umudev_quit(void); diff --git a/src/usb_moded-util.c b/src/usb_moded-util.c index 2e74838..235c118 100644 --- a/src/usb_moded-util.c +++ b/src/usb_moded-util.c @@ -1,7 +1,7 @@ /** * @file usb_moded-util.c * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -34,7 +34,9 @@ * Prototypes * ========================================================================= */ -/* -- util -- */ +/* ------------------------------------------------------------------------- * + * UTIL + * ------------------------------------------------------------------------- */ static int util_query_mode (void); static int util_get_modelist (void); @@ -47,6 +49,12 @@ static int util_set_unhide_mode_config(char *mode); static int util_get_hiddenlist (void); static int util_handle_network (char *network); +/* ------------------------------------------------------------------------- * + * MAIN + * ------------------------------------------------------------------------- */ + +int main(int argc, char *argv[]); + /* ========================================================================= * * Data * ========================================================================= */ diff --git a/src/usb_moded-worker.c b/src/usb_moded-worker.c index e93bb3f..ab3372f 100644 --- a/src/usb_moded-worker.c +++ b/src/usb_moded-worker.c @@ -1,7 +1,7 @@ /** * @file usb_moded-worker.c * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -44,39 +44,44 @@ * Prototypes * ========================================================================= */ -/* -- worker -- */ - -static bool worker_thread_p (void); -bool worker_bailing_out (void); -static bool worker_mode_is_mtp_mode (const char *mode); -static bool worker_is_mtpd_running (void); -static bool worker_stop_mtpd (void); -static bool worker_start_mtpd (void); -static bool worker_switch_to_charging (void); -const char *worker_get_kernel_module (void); -bool worker_set_kernel_module (const char *module); -void worker_clear_kernel_module (void); -mode_list_elem_t *worker_get_usb_mode_data (void); -void worker_set_usb_mode_data (mode_list_elem_t *data); -static const char *worker_get_activated_mode_locked(void); -static bool worker_set_activated_mode_locked(const char *mode); -static const char *worker_get_requested_mode_locked(void); -static bool worker_set_requested_mode_locked(const char *mode); -void worker_request_hardware_mode (const char *mode); -void worker_clear_hardware_mode (void); -static void worker_execute (void); -void worker_switch_to_mode (const char *mode); -static guint worker_add_iowatch (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr); -static void *worker_thread_cb (void *aptr); -static gboolean worker_notify_cb (GIOChannel *chn, GIOCondition cnd, gpointer data); -static bool worker_start_thread (void); -static void worker_stop_thread (void); -static void worker_delete_eventfd (void); -static bool worker_create_eventfd (void); -bool worker_init (void); -void worker_quit (void); -void worker_wakeup (void); -static void worker_notify (void); +/* ------------------------------------------------------------------------- * + * WORKER + * ------------------------------------------------------------------------- */ + +static bool worker_thread_p (void); +bool worker_bailing_out (void); +static bool worker_mode_is_mtp_mode (const char *mode); +static bool worker_is_mtpd_running (void); +static bool worker_mtpd_running_p (void *aptr); +static bool worker_mtpd_stopped_p (void *aptr); +static bool worker_stop_mtpd (void); +static bool worker_start_mtpd (void); +static bool worker_switch_to_charging (void); +const char *worker_get_kernel_module (void); +bool worker_set_kernel_module (const char *module); +void worker_clear_kernel_module (void); +const modedata_t *worker_get_usb_mode_data (void); +modedata_t *worker_dup_usb_mode_data (void); +void worker_set_usb_mode_data (const modedata_t *data); +static const char *worker_get_activated_mode_locked(void); +static bool worker_set_activated_mode_locked(const char *mode); +static const char *worker_get_requested_mode_locked(void); +static bool worker_set_requested_mode_locked(const char *mode); +void worker_request_hardware_mode (const char *mode); +void worker_clear_hardware_mode (void); +static void worker_execute (void); +static void worker_switch_to_mode (const char *mode); +static guint worker_add_iowatch (int fd, bool close_on_unref, GIOCondition cnd, GIOFunc io_cb, gpointer aptr); +static void *worker_thread_cb (void *aptr); +static gboolean worker_notify_cb (GIOChannel *chn, GIOCondition cnd, gpointer data); +static bool worker_start_thread (void); +static void worker_stop_thread (void); +static void worker_delete_eventfd (void); +static bool worker_create_eventfd (void); +bool worker_init (void); +void worker_quit (void); +void worker_wakeup (void); +static void worker_notify (void); /* ========================================================================= * * Data @@ -387,30 +392,56 @@ void worker_clear_kernel_module(void) * ------------------------------------------------------------------------- */ /** Contains the mode data */ -static mode_list_elem_t *worker_mode_data = NULL; +static modedata_t *worker_mode_data = NULL; /** get the usb mode data * - * @return a pointer to the usb mode data + * Note: This function should be called only from the worker thread. * + * @return a pointer to the usb mode data */ -mode_list_elem_t *worker_get_usb_mode_data(void) +const modedata_t *worker_get_usb_mode_data(void) { LOG_REGISTER_CONTEXT; return worker_mode_data; } -/** set the mode_list_elem_t data +/** get clone of the usb mode data * - * @param data mode_list_element pointer + * Caller must release the returned object via #modedata_free(). + * + * @return a pointer to the usb mode data + */ +modedata_t *worker_dup_usb_mode_data(void) +{ + LOG_REGISTER_CONTEXT; + + WORKER_LOCKED_ENTER; + + modedata_t *modedata = modedata_copy(worker_mode_data); + + WORKER_LOCKED_LEAVE; + + return modedata;; +} + +/** set the modedata_t data + * + * Note: This function should be called only from the worker thread, * + * @param data mode_list_element pointer */ -void worker_set_usb_mode_data(mode_list_elem_t *data) +void worker_set_usb_mode_data(const modedata_t *data) { LOG_REGISTER_CONTEXT; - worker_mode_data = data; + WORKER_LOCKED_ENTER; + + modedata_free(worker_mode_data), + worker_mode_data = modedata_copy(data); + + WORKER_LOCKED_LEAVE; } /* ------------------------------------------------------------------------- * @@ -542,12 +573,13 @@ worker_execute(void) * MODE_SWITCH * ------------------------------------------------------------------------- */ -void +static void worker_switch_to_mode(const char *mode) { LOG_REGISTER_CONTEXT; const char *override = 0; + modedata_t *data = 0; /* set return to 1 to be sure to error out if no matching mode is found either */ @@ -581,13 +613,7 @@ worker_switch_to_mode(const char *mode) goto FAILED; } - /* go through all the dynamic modes if the modelist exists*/ - for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) - { - mode_list_elem_t *data = iter->data; - if( strcmp(mode, data->mode_name) ) - continue; - + if( (data = usbmoded_dup_modedata(mode)) ) { log_debug("Matching mode %s found.\n", mode); /* set data before calling any of the dynamic mode functions @@ -678,6 +704,8 @@ worker_switch_to_mode(const char *mode) worker_notify(); + modedata_free(data); + return; } @@ -962,6 +990,9 @@ worker_quit(void) worker_stop_thread(); worker_delete_eventfd(); + + /* Worker thread is stopped and resources can be released. */ + worker_set_usb_mode_data(0); } void diff --git a/src/usb_moded-worker.h b/src/usb_moded-worker.h index db958ac..852e76f 100644 --- a/src/usb_moded-worker.h +++ b/src/usb_moded-worker.h @@ -1,7 +1,7 @@ /** * @file usb_moded-worker.h * - * Copyright (C) 2013-2018 Jolla. All rights reserved. + * Copyright (C) 2013-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Simo Piiroinen @@ -36,19 +36,21 @@ * Prototypes * ========================================================================= */ -/* -- worker -- */ - -bool worker_bailing_out (void); -const char *worker_get_kernel_module (void); -bool worker_set_kernel_module (const char *module); -void worker_clear_kernel_module (void); -mode_list_elem_t *worker_get_usb_mode_data (void); -void worker_set_usb_mode_data (mode_list_elem_t *data); -void worker_request_hardware_mode(const char *mode); -void worker_clear_hardware_mode (void); -void worker_switch_to_mode (const char *mode); -bool worker_init (void); -void worker_quit (void); -void worker_wakeup (void); +/* ------------------------------------------------------------------------- * + * WORKER + * ------------------------------------------------------------------------- */ + +bool worker_bailing_out (void); +const char *worker_get_kernel_module (void); +bool worker_set_kernel_module (const char *module); +void worker_clear_kernel_module (void); +const modedata_t *worker_get_usb_mode_data (void); +modedata_t *worker_dup_usb_mode_data (void); +void worker_set_usb_mode_data (const modedata_t *data); +void worker_request_hardware_mode(const char *mode); +void worker_clear_hardware_mode (void); +bool worker_init (void); +void worker_quit (void); +void worker_wakeup (void); #endif /* USB_MODED_WORKER_H_ */ diff --git a/src/usb_moded.c b/src/usb_moded.c index 99e9d89..cb230b1 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -53,6 +53,7 @@ #include "usb_moded-trigger.h" #include "usb_moded-udev.h" #include "usb_moded-worker.h" +#include "usb_moded-modes.h" #ifdef MEEGOLOCK # include "usb_moded-dsme.h" @@ -102,30 +103,40 @@ * Prototypes * ========================================================================= */ -/* -- usbmoded -- */ - -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[]); +/* ------------------------------------------------------------------------- * + * USBMODED + * ------------------------------------------------------------------------- */ + +GList *usbmoded_get_modelist (void); +void usbmoded_load_modelist (void); +void usbmoded_free_modelist (void); +const modedata_t *usbmoded_get_modedata (const char *modename); +modedata_t *usbmoded_dup_modedata (const char *modename); +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_can_export (void); +bool usbmoded_init_done_p (void); +void usbmoded_set_init_done (bool reached); +void usbmoded_probe_init_done (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[]); + +/* ------------------------------------------------------------------------- * + * MAIN + * ------------------------------------------------------------------------- */ + +int main(int argc, char *argv[]); /* ========================================================================= * * Data @@ -140,6 +151,22 @@ static bool usbmoded_systemd_notify = false; #endif static bool usbmoded_auto_exit = false; +static pthread_mutex_t usbmoded_mutex = PTHREAD_MUTEX_INITIALIZER; + +#define USBMODED_LOCKED_ENTER do {\ + if( pthread_mutex_lock(&usbmoded_mutex) != 0 ) { \ + log_crit("USBMODED LOCK FAILED");\ + _exit(EXIT_FAILURE);\ + }\ +}while(0) + +#define USBMODED_LOCKED_LEAVE do {\ + if( pthread_mutex_unlock(&usbmoded_mutex) != 0 ) { \ + log_crit("USBMODED UNLOCK FAILED");\ + _exit(EXIT_FAILURE);\ + }\ +}while(0) + /* ========================================================================= * * Functions * ========================================================================= */ @@ -148,8 +175,18 @@ static bool usbmoded_auto_exit = false; * MODELIST * ------------------------------------------------------------------------- */ +/** List of mode data items read from configuration files + * + * Note: Worker thread should access this only via #usbmoded_dup_modedata(). + */ static GList *usbmoded_modelist = 0; +/** Get list of dynamic mode data items + * + * Note: This function should be called only from the main thread. + * + * @returns List of mode data objects, or NULL + */ GList * usbmoded_get_modelist(void) { @@ -158,27 +195,92 @@ usbmoded_get_modelist(void) return usbmoded_modelist; } +/** Load dynamic mode data items + * + * Note: This function should be called only from the main thread. + */ void usbmoded_load_modelist(void) { LOG_REGISTER_CONTEXT; + USBMODED_LOCKED_ENTER; + if( !usbmoded_modelist ) { log_notice("load modelist"); - usbmoded_modelist = dynconfig_read_mode_list(usbmoded_get_diag_mode()); + usbmoded_modelist = modelist_load(usbmoded_get_diag_mode()); } + + USBMODED_LOCKED_LEAVE; } +/** Free dynamic mode data items + * + * Note: This function should be called only from the main thread. + */ void usbmoded_free_modelist(void) { LOG_REGISTER_CONTEXT; + USBMODED_LOCKED_ENTER; + if( usbmoded_modelist ) { log_notice("free modelist"); - dynconfig_free_mode_list(usbmoded_modelist), + modelist_free(usbmoded_modelist), usbmoded_modelist = 0; } + + USBMODED_LOCKED_LEAVE; +} + +/** Lookup dynamic mode data by name + * + * Note: This function should be called only from the main thread. + * + * @param modename Name of mode to lookup + * + * @return Mode data object, or NULL + */ +const modedata_t * +usbmoded_get_modedata(const char *modename) +{ + LOG_REGISTER_CONTEXT; + + modedata_t *modedata = 0; + + for( GList *iter = usbmoded_get_modelist(); iter; iter = g_list_next(iter) ) { + modedata_t *data = iter->data; + if( !g_strcmp0(data->mode_name, modename) ) { + modedata = data; + break; + } + } + return modedata; +} + +/** Lookup and clone dynamic mode data by name + * + * Note: This function is safe to call from worker thread too. + * + * Caller must release the returned object via #modedata_free(). + * + * @param modename Name of mode to lookup + * + * @return Mode data object, or NULL + */ +modedata_t * +usbmoded_dup_modedata(const char *modename) +{ + LOG_REGISTER_CONTEXT; + + USBMODED_LOCKED_ENTER; + + modedata_t *modedata = modedata_copy(usbmoded_get_modedata(modename)); + + USBMODED_LOCKED_LEAVE; + + return modedata; } /* ------------------------------------------------------------------------- * @@ -467,14 +569,57 @@ void usbmoded_handle_signal(int signum) } else if( signum == SIGHUP ) { - /* free and read in modelist again */ + /* Reload mode list */ + log_debug("reloading dynamic mode configuration"); usbmoded_free_modelist(); usbmoded_load_modelist(); + /* If default mode selection became invalid, + * revert setting to "ask" */ + gchar *config = config_get_mode_setting(); + if( g_strcmp0(config, MODE_ASK) && + common_valid_mode(config) ) { + log_warning("default mode '%s' is not valid, reset to '%s'", + config, MODE_ASK); + config_set_mode_setting(MODE_ASK); + } + else { + log_debug("default mode '%s' is still valid", config); + } + g_free(config); + + /* If current mode became invalid, select appropriate mode. + * + * Use target mode so that we catch also situations where + * we are making transition to invalid state. + */ + const char *current = control_get_target_mode(); + if( common_modename_is_internal(current) ) { + /* Internal modes are not affected by configuration + * file changes - no changes required. */ + log_debug("current mode '%s' is internal", current); + } + else if( common_valid_mode(current) ) { + /* Dynamic mode that is no longer valid - choose + * something else. */ + log_warning("current mode '%s' is not valid, re-evaluating", + current); + control_select_usb_mode(); + } + else { + /* Dynamic mode that is still valid - do nothing. + * + * Note: While the mode details /might/ have changed, + * skipping immediate usb reprogramming is assumed to + * be less harmful than potentially cutting developer + * mode connection during upgrade, etc. */ + log_debug("current mode '%s' is still valid", current); + } + + /* Signal availability */ + log_debug("broadcast mode availability lists"); common_send_supported_modes_signal(); common_send_available_modes_signal(); - - // FIXME invalidate current mode } else { @@ -520,7 +665,7 @@ static bool usbmoded_init(void) /* DSME listener maintains in-user-mode state and is relevant * only when MEEGOLOCK configure option has been chosen. */ #ifdef MEEGOLOCK - if( !dsme_listener_start() ) { + if( !dsme_start_listener() ) { log_crit("dsme tracking could not be started"); goto EXIT; } @@ -674,7 +819,7 @@ static void usbmoded_cleanup(void) /* Stop tracking device state */ #ifdef MEEGOLOCK - dsme_listener_stop(); + dsme_stop_listener(); #endif /* Stop udev listener */ diff --git a/src/usb_moded.h b/src/usb_moded.h index 6ab0094..a713870 100644 --- a/src/usb_moded.h +++ b/src/usb_moded.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2010 Nokia Corporation. All rights reserved. - * Copyright (C) 2012-2018 Jolla. All rights reserved. + * Copyright (C) 2012-2019 Jolla. All rights reserved. * * @author: Philippe De Swert * @author: Philippe De Swert @@ -32,6 +32,8 @@ # include "../config.h" // NOTRIM # endif +# include "usb_moded-dyn-config.h" + # include # include @@ -57,27 +59,37 @@ (USB_MODED_SUSPEND_DELAY_DEFAULT_MS * 2) /* ========================================================================= * - * Functions + * Prototypes * ========================================================================= */ -/* -- usbmoded -- */ - -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); +/* ------------------------------------------------------------------------- * + * USBMODED + * ------------------------------------------------------------------------- */ + +GList *usbmoded_get_modelist (void); +void usbmoded_load_modelist (void); +void usbmoded_free_modelist (void); +const modedata_t *usbmoded_get_modedata (const char *modename); +modedata_t *usbmoded_dup_modedata (const char *modename); +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_can_export (void); +bool usbmoded_init_done_p (void); +void usbmoded_set_init_done (bool reached); +void usbmoded_probe_init_done (void); +void usbmoded_exit_mainloop (int exitcode); +void usbmoded_handle_signal (int signum); + +/* ------------------------------------------------------------------------- * + * MAIN + * ------------------------------------------------------------------------- */ + +int main(int argc, char *argv[]); #endif /* USB_MODED_H_ */