diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2017-01-13 14:15:40 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2017-01-16 11:24:16 +0100 |
commit | f8c5c6f48b07fce4d6deeb8c4b10aaf393ca8264 (patch) | |
tree | 5290d6e473b2cf5412529c3d76f5272de686c95d /src/qmi-firmware-update/qfu-main.c | |
parent | bdc0a1e71e18419dd15f8d6865425cb6884e77c0 (diff) | |
download | external_libqmi-f8c5c6f48b07fce4d6deeb8c4b10aaf393ca8264.zip external_libqmi-f8c5c6f48b07fce4d6deeb8c4b10aaf393ca8264.tar.gz external_libqmi-f8c5c6f48b07fce4d6deeb8c4b10aaf393ca8264.tar.bz2 |
qmi-firmware-update: new 'device selection' object
This new object contains the logic to select which device needs to be
used as target of the firmware upgrade.
Diffstat (limited to 'src/qmi-firmware-update/qfu-main.c')
-rw-r--r-- | src/qmi-firmware-update/qfu-main.c | 213 |
1 files changed, 46 insertions, 167 deletions
diff --git a/src/qmi-firmware-update/qfu-main.c b/src/qmi-firmware-update/qfu-main.c index af83186..df6733d 100644 --- a/src/qmi-firmware-update/qfu-main.c +++ b/src/qmi-firmware-update/qfu-main.c @@ -31,6 +31,7 @@ #include "qfu-log.h" #include "qfu-operation.h" +#include "qfu-device-selection.h" #include "qfu-udev-helpers.h" #define PROGRAM_NAME "qmi-firmware-update" @@ -44,10 +45,11 @@ static guint busnum; static guint devnum; static guint16 vid; static guint16 pid; +static gchar *cdc_wdm_str; +static gchar *tty_str; /* Update */ static gboolean action_update_flag; -static gchar *cdc_wdm_str; static gchar *firmware_version_str; static gchar *config_version_str; static gchar *carrier_str; @@ -57,11 +59,9 @@ static gboolean force_flag; /* Reset */ static gboolean action_reset_flag; -static gchar *at_serial_str; /* Update (QDL mode) */ static gboolean action_update_qdl_flag; -static gchar *qdl_serial_str; /* Verify */ static gboolean action_verify_flag; @@ -178,6 +178,14 @@ static GOptionEntry context_selection_entries[] = { "Select device by device vendor and product id (in hexadecimal).", "VID[:PID]" }, + { "cdc-wdm", 'w', 0, G_OPTION_ARG_FILENAME, &cdc_wdm_str, + "Select device by QMI/MBIM cdc-wdm device path (e.g. /dev/cdc-wdm0).", + "[PATH]" + }, + { "tty", 't', 0, G_OPTION_ARG_FILENAME, &tty_str, + "Select device by serial device path (e.g. /dev/ttyUSB2).", + "[PATH]" + }, { NULL } }; @@ -186,10 +194,6 @@ static GOptionEntry context_update_entries[] = { "Launch firmware update process.", NULL }, - { "cdc-wdm", 'w', 0, G_OPTION_ARG_FILENAME, &cdc_wdm_str, - "Select device by QMI/MBIM cdc-wdm device path (e.g. /dev/cdc-wdm0).", - "[PATH]" - }, { "firmware-version", 'f', 0, G_OPTION_ARG_STRING, &firmware_version_str, "Firmware version (e.g. '05.05.58.00').", "[VERSION]", @@ -222,10 +226,6 @@ static GOptionEntry context_reset_entries[] = { "Reset device into QDL download mode.", NULL }, - { "at-serial", 'a', 0, G_OPTION_ARG_FILENAME, &at_serial_str, - "Select device by AT serial device path (e.g. /dev/ttyUSB2).", - "[PATH]" - }, { NULL } }; @@ -234,10 +234,6 @@ static GOptionEntry context_update_qdl_entries[] = { "Launch firmware update process in QDL mode.", NULL }, - { "qdl-serial", 'q', 0, G_OPTION_ARG_FILENAME, &qdl_serial_str, - "Select device by QDL serial device path (e.g. /dev/ttyUSB0).", - "[PATH]" - }, { NULL } }; @@ -295,9 +291,9 @@ print_version (void) g_print ("\n" PROGRAM_NAME " " PROGRAM_VERSION "\n" "\n" - " Copyright (C) 2016 Bjørn Mork\n" - " Copyright (C) 2016 Zodiac Inflight Innovations\n" - " Copyright (C) 2016 Aleksander Morgado\n" + " Copyright (C) 2016-2017 Bjørn Mork\n" + " Copyright (C) 2016-2017 Zodiac Inflight Innovations\n" + " Copyright (C) 2016-2017 Aleksander Morgado\n" "\n" "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n" "This is free software: you are free to change and redistribute it.\n" @@ -445,122 +441,14 @@ print_help_examples (void) /*****************************************************************************/ -static gboolean -validate_inputs (const char *manual) -{ - if (!manual && !vid && !pid && !busnum && !devnum) { - g_printerr ("error: device not specified\n"); - return FALSE; - } - if (manual && (vid != 0 || pid != 0)) { - g_printerr ("error: cannot specify device path and vid:pid lookup\n"); - return FALSE; - } - - if (manual && (busnum != 0 || devnum != 0)) { - g_printerr ("error: cannot specify device path and busnum:devnum lookup\n"); - return FALSE; - } - - if ((vid != 0 || pid != 0) && (busnum != 0 || devnum != 0)) { - g_printerr ("error: cannot specify busnum:devnum and vid:pid lookups\n"); - return FALSE; - } - - return TRUE; -} - -static gchar * -select_single_path (const char *manual, - QfuUdevHelperDeviceType type) -{ - gchar *path = NULL; - GError *error = NULL; - gchar *sysfs_path = NULL; - GList *list = NULL; - - if (!validate_inputs (manual)) - goto out; - - if (manual) { - path = g_strdup (manual); - goto out; - } - - /* lookup sysfs path */ - sysfs_path = qfu_udev_helper_find_by_device_info (vid, pid, busnum, devnum, &error); - if (!sysfs_path) { - g_printerr ("error: %s\n", error->message); - g_error_free (error); - goto out; - } - - list = qfu_udev_helper_list_devices (type, sysfs_path); - if (!list) { - g_printerr ("error: no devices found in sysfs path: %s\n", sysfs_path); - goto out; - } - - path = g_file_get_path (G_FILE (list->data)); - -out: - if (list) - g_list_free_full (list, (GDestroyNotify) g_object_unref); - - return path; -} - -static gchar ** -select_multiple_paths (const char *manual, - QfuUdevHelperDeviceType type) -{ - GError *error = NULL; - gchar *sysfs_path = NULL; - GList *list = NULL; - GList *l; - gchar **paths = NULL; - guint i; - - if (!validate_inputs (manual)) - goto out; - - if (manual) { - paths = g_strsplit (manual, ",", -1); - goto out; - } - - /* lookup sysfs path */ - sysfs_path = qfu_udev_helper_find_by_device_info (vid, pid, busnum, devnum, &error); - if (!sysfs_path) { - g_printerr ("error: %s\n", error->message); - g_error_free (error); - goto out; - } - - list = qfu_udev_helper_list_devices (type, sysfs_path); - if (!list) { - g_printerr ("error: no devices found in sysfs path: %s\n", sysfs_path); - goto out; - } - - paths = g_new0 (gchar *, g_list_length (list) + 1); - for (l = list, i = 0; l; l = g_list_next (l), i++) - paths[i] = g_file_get_path (G_FILE (l->data)); - -out: - if (list) - g_list_free_full (list, (GDestroyNotify) g_object_unref); - - return paths; -} - int main (int argc, char **argv) { - GError *error = NULL; - GOptionContext *context; - GOptionGroup *group; - guint n_actions; - gboolean result = FALSE; + GError *error = NULL; + GOptionContext *context; + GOptionGroup *group; + guint n_actions; + gboolean result = FALSE; + QfuDeviceSelection *device_selection = NULL; setlocale (LC_ALL, ""); @@ -640,52 +528,41 @@ int main (int argc, char **argv) goto out; } + /* device selection must be performed for update and reset operations */ + if (action_update_flag || action_update_qdl_flag || action_reset_flag) { + device_selection = qfu_device_selection_new (cdc_wdm_str, tty_str, vid, pid, busnum, devnum, &error); + if (!device_selection) { + g_printerr ("error: couldn't select device:: %s\n", error->message); + g_error_free (error); + goto out; + } + } + /* Run */ if (action_update_flag) { - gchar *path; - - path = select_single_path (cdc_wdm_str, QFU_UDEV_HELPER_DEVICE_TYPE_CDC_WDM); - if (path) { - g_debug ("using cdc-wdm device: %s", path); - result = qfu_operation_update_run ((const gchar **) image_strv, - path, - firmware_version_str, - config_version_str, - carrier_str, - device_open_proxy_flag, - device_open_mbim_flag, - force_flag); - g_free (path); - } + g_assert (QFU_IS_DEVICE_SELECTION (device_selection)); + result = qfu_operation_update_run ((const gchar **) image_strv, + device_selection, + firmware_version_str, + config_version_str, + carrier_str, + device_open_proxy_flag, + device_open_mbim_flag, + force_flag); goto out; } if (action_update_qdl_flag) { - gchar *path; - - path = select_single_path (qdl_serial_str, QFU_UDEV_HELPER_DEVICE_TYPE_TTY); - if (path) { - g_debug ("using tty device: %s", path); - result = qfu_operation_update_qdl_run ((const gchar **) image_strv, - path); - g_free (path); - } + g_assert (QFU_IS_DEVICE_SELECTION (device_selection)); + result = qfu_operation_update_qdl_run ((const gchar **) image_strv, + device_selection); goto out; } if (action_reset_flag) { - gchar **paths; - - paths = select_multiple_paths (at_serial_str, QFU_UDEV_HELPER_DEVICE_TYPE_TTY); - if (paths) { - guint i; - - for (i = 0; paths[i]; i++) - g_debug ("using tty device #%u: %s", i, paths[i]); - result = qfu_operation_reset_run ((const gchar **) paths); - g_strfreev (paths); - } + g_assert (QFU_IS_DEVICE_SELECTION (device_selection)); + result = qfu_operation_reset_run (device_selection); goto out; } @@ -700,6 +577,8 @@ out: /* Clean exit for a clean memleak report */ if (context) g_option_context_free (context); + if (device_selection) + g_object_unref (device_selection); return (result ? EXIT_SUCCESS : EXIT_FAILURE); } |