diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2016-11-29 23:22:29 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2017-01-16 11:24:12 +0100 |
commit | 661e69663bfffc8f79e4015419022a8f880933eb (patch) | |
tree | 9b7ee406464542d8c13b331cda1255df8e45bad7 /src/qmi-firmware-update/qfu-main.c | |
parent | f535ed61f62efb17c977a812ef4688db622712b8 (diff) | |
download | external_libqmi-661e69663bfffc8f79e4015419022a8f880933eb.zip external_libqmi-661e69663bfffc8f79e4015419022a8f880933eb.tar.gz external_libqmi-661e69663bfffc8f79e4015419022a8f880933eb.tar.bz2 |
qmi-firmware-update: implement download operation in separate file
Diffstat (limited to 'src/qmi-firmware-update/qfu-main.c')
-rw-r--r-- | src/qmi-firmware-update/qfu-main.c | 183 |
1 files changed, 66 insertions, 117 deletions
diff --git a/src/qmi-firmware-update/qfu-main.c b/src/qmi-firmware-update/qfu-main.c index b703fb5..9596e86 100644 --- a/src/qmi-firmware-update/qfu-main.c +++ b/src/qmi-firmware-update/qfu-main.c @@ -28,19 +28,19 @@ #include <glib.h> #include <glib/gprintf.h> -#include <glib-unix.h> #include <gio/gio.h> #include <libqmi-glib.h> -#include "qfu-updater.h" +#include "qfu-operation.h" #define PROGRAM_NAME "qmi-firmware-update" #define PROGRAM_VERSION PACKAGE_VERSION /*****************************************************************************/ -/* Main options */ +/* Options */ +/* Download */ static gchar *device_str; static gchar **image_strv; static gchar *firmware_version_str; @@ -48,11 +48,14 @@ static gchar *config_version_str; static gchar *carrier_str; static gboolean device_open_proxy_flag; static gboolean device_open_mbim_flag; -static gboolean verbose_flag; -static gboolean silent_flag; -static gboolean version_flag; -static GOptionEntry context_main_entries[] = { +/* Main */ +static gboolean verbose_flag; +static gboolean silent_flag; +static gboolean version_flag; +static gboolean help_flag; + +static GOptionEntry context_download_entries[] = { { "device", 'd', 0, G_OPTION_ARG_FILENAME, &device_str, "Specify device path.", "[PATH]" @@ -81,6 +84,10 @@ static GOptionEntry context_main_entries[] = { "Open an MBIM device with EXT_QMUX support.", NULL }, + { NULL } +}; + +static GOptionEntry context_main_entries[] = { { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag, "Run action with verbose logs, including the debug ones.", NULL @@ -93,11 +100,15 @@ static GOptionEntry context_main_entries[] = { "Print version.", NULL }, + { "help", 'h', 0, G_OPTION_ARG_NONE, &help_flag, + "Show help", + NULL + }, { NULL } }; static const gchar *context_description = - " E.g.:\n" + " E.g. a download operation:\n" " $ sudo " PROGRAM_NAME " \\\n" " --verbose \\\n" " --device /dev/cdc-wdm4 \\\n" @@ -109,37 +120,6 @@ static const gchar *context_description = "\n"; /*****************************************************************************/ -/* Runtime globals */ - -GMainLoop *loop; -GCancellable *cancellable; -gint exit_status = EXIT_FAILURE; - -/*****************************************************************************/ -/* Signal handlers */ - -static gboolean -signals_handler (gpointer psignum) -{ - /* Ignore consecutive requests of cancellation */ - if (!g_cancellable_is_cancelled (cancellable)) { - g_printerr ("cancelling the operation...\n"); - g_cancellable_cancel (cancellable); - /* Re-set the signal handler to allow main loop cancellation on - * second signal */ - g_unix_signal_add (GPOINTER_TO_INT (psignum), (GSourceFunc) signals_handler, psignum); - return FALSE; - } - - if (g_main_loop_is_running (loop)) { - g_printerr ("cancelling the main loop...\n"); - g_main_loop_quit (loop); - } - - return FALSE; -} - -/*****************************************************************************/ /* Logging output */ static void @@ -196,10 +176,9 @@ log_handler (const gchar *log_domain, } /*****************************************************************************/ -/* Version */ static void -print_version_and_exit (void) +print_version (void) { g_print ("\n" PROGRAM_NAME " " PROGRAM_VERSION "\n" @@ -210,36 +189,26 @@ print_version_and_exit (void) "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n" "\n"); - exit (EXIT_SUCCESS); } -/*****************************************************************************/ - static void -run_ready (QfuUpdater *updater, - GAsyncResult *res) +print_help (GOptionContext *context) { - GError *error = NULL; - - if (!qfu_updater_run_finish (updater, res, &error)) { - g_printerr ("error: firmware update operation finished: %s\n", error->message); - g_error_free (error); - } else { - g_print ("firmware update operation finished successfully\n"); - exit_status = EXIT_SUCCESS; - } + gchar *str; - g_idle_add ((GSourceFunc) g_main_loop_quit, loop); + /* Always print --help-all */ + str = g_option_context_get_help (context, FALSE, NULL); + g_print ("%s", str); + g_free (str); } int main (int argc, char **argv) { GError *error = NULL; GOptionContext *context; - QfuUpdater *updater; - GFile *device_file; - GList *image_file_list = NULL; - guint i; + GOptionGroup *group; + guint n_actions; + gboolean result = FALSE; setlocale (LC_ALL, ""); @@ -247,81 +216,60 @@ int main (int argc, char **argv) /* Setup option context, process it and destroy it */ context = g_option_context_new ("- Update firmware in QMI devices"); + + group = g_option_group_new ("download", "Download options", "", NULL, NULL); + g_option_group_add_entries (group, context_download_entries); + g_option_context_add_group (context, group); + g_option_context_add_main_entries (context, context_main_entries, NULL); - g_option_context_set_description (context, context_description); + g_option_context_set_description (context, context_description); + g_option_context_set_help_enabled (context, FALSE); + if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("error: couldn't parse option context: %s\n", error->message); g_error_free (error); goto out; } - if (version_flag) - print_version_and_exit (); - - g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL); - g_log_set_handler ("Qmi", G_LOG_LEVEL_MASK, log_handler, NULL); - if (verbose_flag) - qmi_utils_set_traces_enabled (TRUE); - - /* No device path given? */ - if (!device_str) { - g_printerr ("error: no device path specified\n"); + if (version_flag) { + print_version (); + result = TRUE; goto out; } - /* No firmware version given? */ - if (!firmware_version_str) { - g_printerr ("error: no firmware version specified\n"); + if (help_flag) { + print_help (context); + result = TRUE; goto out; } - /* No config version given? */ - if (!config_version_str) { - g_printerr ("error: no config version specified\n"); - goto out; - } + g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL); + g_log_set_handler ("Qmi", G_LOG_LEVEL_MASK, log_handler, NULL); + if (verbose_flag) + qmi_utils_set_traces_enabled (TRUE); - /* No carrier given? */ - if (!carrier_str) { - g_printerr ("error: no carrier specified\n"); + /* We don't allow multiple actions at the same time */ + n_actions = (!!image_strv); + if (n_actions == 0) { + g_printerr ("error: no actions specified\n"); goto out; } - - /* No images given? */ - if (!image_strv) { - g_printerr ("error: no image path(s) specified\n"); + if (n_actions > 1) { + g_printerr ("error: too many actions specified\n"); goto out; } - /* Create runtime context */ - loop = g_main_loop_new (NULL, FALSE); - cancellable = g_cancellable_new (); - exit_status = EXIT_SUCCESS; - - /* Setup signals */ - g_unix_signal_add (SIGINT, (GSourceFunc)signals_handler, GINT_TO_POINTER (SIGINT)); - g_unix_signal_add (SIGHUP, (GSourceFunc)signals_handler, GINT_TO_POINTER (SIGHUP)); - g_unix_signal_add (SIGTERM, (GSourceFunc)signals_handler, GINT_TO_POINTER (SIGTERM)); - - /* Create list of image files */ - for (i = 0; image_strv[i]; i++) - image_file_list = g_list_append (image_file_list, g_file_new_for_commandline_arg (image_strv[i])); - - /* Create updater */ - device_file = g_file_new_for_commandline_arg (device_str); - updater = qfu_updater_new (device_file, - firmware_version_str, - config_version_str, - carrier_str, - image_file_list, - device_open_proxy_flag, - device_open_mbim_flag); - g_object_unref (device_file); - g_list_free_full (image_file_list, (GDestroyNotify) g_object_unref); - - /* Run! */ - qfu_updater_run (updater, cancellable, (GAsyncReadyCallback) run_ready, NULL); - g_main_loop_run (loop); + /* Run operation */ + if (image_strv) + result = qfu_operation_download_run (device_str, + firmware_version_str, + config_version_str, + carrier_str, + (const gchar **) image_strv, + device_open_proxy_flag, + device_open_mbim_flag); + else + g_assert_not_reached (); out: /* Clean exit for a clean memleak report */ @@ -334,5 +282,6 @@ out: if (loop) g_main_loop_unref (loop); - return (exit_status); + + return (result ? EXIT_SUCCESS : EXIT_FAILURE); } |