Fix FuCfuOffer XML roundtrip

This commit is contained in:
Richard Hughes 2022-07-01 13:02:35 +01:00
parent 5cf63a0741
commit 95f7d23d58
5 changed files with 62 additions and 8 deletions

View File

@ -12,6 +12,7 @@
#include "fu-cfu-offer.h"
#include "fu-common.h"
#include "fu-mem.h"
#include "fu-string.h"
/**
* FuCfuOffer:
@ -495,17 +496,22 @@ fu_cfu_offer_build(FuFirmware *firmware, XbNode *n, GError **error)
FuCfuOffer *self = FU_CFU_OFFER(firmware);
FuCfuOfferPrivate *priv = GET_PRIVATE(self);
guint64 tmp;
const gchar *tmpb;
/* optional properties */
tmp = xb_node_query_text_as_uint(n, "segment_number", NULL);
if (tmp != G_MAXUINT64 && tmp <= G_MAXUINT8)
priv->segment_number = tmp;
tmp = xb_node_query_text_as_uint(n, "force_immediate_reset", NULL);
if (tmp != G_MAXUINT64 && tmp <= G_MAXUINT8)
priv->force_immediate_reset = tmp;
tmp = xb_node_query_text_as_uint(n, "force_ignore_version", NULL);
if (tmp != G_MAXUINT64 && tmp <= G_MAXUINT8)
priv->force_ignore_version = tmp;
tmpb = xb_node_query_text(n, "force_immediate_reset", NULL);
if (tmpb != NULL) {
if (!fu_strtobool(tmpb, &priv->force_immediate_reset, error))
return FALSE;
}
tmpb = xb_node_query_text(n, "force_ignore_version", NULL);
if (tmpb != NULL) {
if (!fu_strtobool(tmpb, &priv->force_ignore_version, error))
return FALSE;
}
tmp = xb_node_query_text_as_uint(n, "component_id", NULL);
if (tmp != G_MAXUINT64 && tmp <= G_MAXUINT8)
priv->component_id = tmp;

View File

@ -90,6 +90,51 @@ fu_strtoull(const gchar *str, guint64 *value, guint64 min, guint64 max, GError *
return TRUE;
}
/**
* fu_strtobool:
* @str: a string, e.g. `true`
* @value: (out) (nullable): parsed value
* @error: (nullable): optional return location for an error
*
* Converts a string value to a boolean. Only `true` and `false` are accepted values.
*
* Returns: %TRUE if the value was parsed correctly, or %FALSE for error
*
* Since: 1.8.2
**/
gboolean
fu_strtobool(const gchar *str, gboolean *value, GError **error)
{
/* sanity check */
if (str == NULL) {
g_set_error_literal(error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"cannot parse NULL");
return FALSE;
}
/* be super strict */
if (g_strcmp0(str, "true") == 0) {
if (value != NULL)
*value = TRUE;
return TRUE;
}
if (g_strcmp0(str, "false") == 0) {
if (value != NULL)
*value = FALSE;
return TRUE;
}
/* invalid */
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"cannot parse %s as boolean, expected true|false",
str);
return FALSE;
}
/**
* fu_strstrip:
* @str: a string, e.g. ` test `

View File

@ -23,6 +23,8 @@ gchar *
fu_strsafe(const gchar *str, gsize maxsz);
gboolean
fu_strtoull(const gchar *str, guint64 *value, guint64 min, guint64 max, GError **error);
gboolean
fu_strtobool(const gchar *str, gboolean *value, GError **error);
gchar *
fu_strstrip(const gchar *str);
gsize

View File

@ -1002,6 +1002,7 @@ LIBFWUPDPLUGIN_1.8.2 {
fu_strsplit;
fu_strsplit_full;
fu_strstrip;
fu_strtobool;
fu_strtoull;
fu_strwidth;
fu_sum16;

View File

@ -1,7 +1,7 @@
<firmware gtype="FuCfuOffer">
<segment_number>0x42</segment_number>
<force_immediate_reset>1</force_immediate_reset>
<force_ignore_version>1</force_ignore_version>
<force_immediate_reset>true</force_immediate_reset>
<force_ignore_version>true</force_ignore_version>
<component_id>0xAB</component_id>
<token>0xCD</token>
<version_raw>0x4567</version_raw>