Add 'fwupdmgr get-topology' to show logical device tree

This commit is contained in:
Richard Hughes 2018-05-02 08:14:51 +01:00
parent 5e447293fa
commit 42728c4d5e
2 changed files with 81 additions and 0 deletions

View File

@ -12,6 +12,7 @@ _fwupdmgr_cmd_list=(
'get-releases'
'get-remotes'
'get-results'
'get-topology'
'get-updates'
'hwids'
'install'

View File

@ -523,6 +523,80 @@ fu_util_modify_remote (FuUtilPrivate *priv,
NULL, error);
}
static void
fu_util_build_device_tree (GNode *root, GPtrArray *devs, FwupdDevice *dev)
{
for (guint i = 0; i < devs->len; i++) {
FwupdDevice *dev_tmp = g_ptr_array_index (devs, i);
if (fwupd_device_get_parent (dev_tmp) == dev) {
GNode *child = g_node_append_data (root, dev_tmp);
fu_util_build_device_tree (child, devs, dev_tmp);
}
}
}
static gboolean
fu_util_print_device_tree (GNode *n, gpointer data)
{
FwupdDevice *dev = FWUPD_DEVICE (n->data);
const gchar *name;
g_autoptr(GString) str = g_string_new (NULL);
/* root node */
if (dev == NULL) {
g_print ("\n");
return FALSE;
}
/* add previous branches */
for (GNode *c = n->parent; c->parent != NULL; c = c->parent) {
if (g_node_next_sibling (c) == NULL)
g_string_prepend (str, " ");
else
g_string_prepend (str, "");
}
/* add this branch */
if (g_node_last_sibling (n) == n)
g_string_append (str, "└─ ");
else
g_string_append (str, "├─ ");
/* dump to the console */
name = fwupd_device_get_name (dev);
if (name == NULL)
name = "Unknown device";
g_string_append (str, name);
for (guint i = strlen (name) + 2 * g_node_depth (n); i < 45; i++)
g_string_append_c (str, ' ');
g_print ("%s %s\n", str->str, fu_device_get_id (dev));
return FALSE;
}
static gboolean
fu_util_get_topology (FuUtilPrivate *priv, gchar **values, GError **error)
{
g_autoptr(GNode) root = g_node_new (NULL);
g_autoptr(GPtrArray) devs = NULL;
/* get results from daemon */
devs = fwupd_client_get_devices (priv->client, NULL, error);
if (devs == NULL)
return FALSE;
/* print */
if (devs->len == 0) {
/* TRANSLATORS: nothing attached that can be upgraded */
g_print ("%s\n", _("No hardware detected with firmware update capability"));
return TRUE;
}
fu_util_build_device_tree (root, devs, NULL);
g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
fu_util_print_device_tree, priv);
return TRUE;
}
static gboolean
fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error)
{
@ -2303,6 +2377,12 @@ main (int argc, char *argv[])
/* TRANSLATORS: command description */
_("Get all devices that support firmware updates"),
fu_util_get_devices);
fu_util_add (priv->cmd_array,
"get-topology",
NULL,
/* TRANSLATORS: command description */
_("Get all devices according to the system topology"),
fu_util_get_topology);
fu_util_add (priv->cmd_array,
"hwids",
NULL,