Convert FuChunk from a C structure to an object

This commit is contained in:
Richard Hughes 2021-01-28 16:49:34 +00:00
parent 774b37cd65
commit a3f8fc30ac
4 changed files with 50 additions and 31 deletions

View File

@ -20,6 +20,17 @@
* *
*/ */
struct _FuChunk {
GObject parent_instance;
guint32 idx;
guint32 page;
guint32 address;
const guint8 *data;
guint32 data_sz;
};
G_DEFINE_TYPE (FuChunk, fu_chunk, G_TYPE_OBJECT)
/** /**
* fu_chunk_get_idx: * fu_chunk_get_idx:
* @self: a #FuChunk * @self: a #FuChunk
@ -145,7 +156,7 @@ fu_chunk_get_bytes (FuChunk *self)
} }
/** /**
* fu_chunk_new: (skip): * fu_chunk_new:
* @idx: the packet number * @idx: the packet number
* @page: the hardware memory page * @page: the hardware memory page
* @address: the address *within* the page * @address: the address *within* the page
@ -165,18 +176,18 @@ fu_chunk_new (guint32 idx,
const guint8 *data, const guint8 *data,
guint32 data_sz) guint32 data_sz)
{ {
FuChunk *item = g_new0 (FuChunk, 1); FuChunk *self = g_object_new (FU_TYPE_CHUNK, NULL);
item->idx = idx; self->idx = idx;
item->page = page; self->page = page;
item->address = address; self->address = address;
item->data = data; self->data = data;
item->data_sz = data_sz; self->data_sz = data_sz;
return item; return self;
} }
/** /**
* fu_chunk_to_string: * fu_chunk_to_string:
* @item: a #FuChunk * @self: a #FuChunk
* *
* Converts the chunked packet to a string representation. * Converts the chunked packet to a string representation.
* *
@ -185,12 +196,12 @@ fu_chunk_new (guint32 idx,
* Since: 1.1.2 * Since: 1.1.2
**/ **/
gchar * gchar *
fu_chunk_to_string (FuChunk *item) fu_chunk_to_string (FuChunk *self)
{ {
g_autoptr(GString) str = g_string_new (NULL); g_autoptr(GString) str = g_string_new (NULL);
if (item->data != NULL) { if (self->data != NULL) {
for (guint32 i = 0; i < item->data_sz; i++) { for (guint32 i = 0; i < self->data_sz; i++) {
gchar tmp = (gchar) item->data[i]; gchar tmp = (gchar) self->data[i];
if (tmp == 0x00) if (tmp == 0x00)
break; break;
g_string_append_c (str, g_ascii_isalnum (tmp) ? tmp : '?'); g_string_append_c (str, g_ascii_isalnum (tmp) ? tmp : '?');
@ -198,10 +209,10 @@ fu_chunk_to_string (FuChunk *item)
} }
return g_strdup_printf ("#%02" G_GUINT32_FORMAT ": page:%02x " return g_strdup_printf ("#%02" G_GUINT32_FORMAT ": page:%02x "
"addr:%04x len:%02" G_GUINT32_FORMAT " %s", "addr:%04x len:%02" G_GUINT32_FORMAT " %s",
item->idx, self->idx,
(guint) item->page, (guint) self->page,
(guint) item->address, (guint) self->address,
item->data_sz, self->data_sz,
str->str); str->str);
} }
@ -220,15 +231,15 @@ fu_chunk_array_to_string (GPtrArray *chunks)
{ {
GString *str = g_string_new (NULL); GString *str = g_string_new (NULL);
for (guint i = 0; i < chunks->len; i++) { for (guint i = 0; i < chunks->len; i++) {
FuChunk *item = g_ptr_array_index (chunks, i); FuChunk *chk = g_ptr_array_index (chunks, i);
g_autofree gchar *tmp = fu_chunk_to_string (item); g_autofree gchar *tmp = fu_chunk_to_string (chk);
g_string_append_printf (str, "%s\n", tmp); g_string_append_printf (str, "%s\n", tmp);
} }
return g_string_free (str, FALSE); return g_string_free (str, FALSE);
} }
/** /**
* fu_chunk_array_new: (skip): * fu_chunk_array_new:
* @data: a linear blob of memory, or %NULL * @data: a linear blob of memory, or %NULL
* @data_sz: size of @data_sz * @data_sz: size of @data_sz
* @addr_start: the hardware address offset, or 0 * @addr_start: the hardware address offset, or 0
@ -256,7 +267,7 @@ fu_chunk_array_new (const guint8 *data,
g_return_val_if_fail (data_sz > 0, NULL); g_return_val_if_fail (data_sz > 0, NULL);
segments = g_ptr_array_new_with_free_func (g_free); segments = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
for (idx = 1; idx < data_sz; idx++) { for (idx = 1; idx < data_sz; idx++) {
guint32 page = 0; guint32 page = 0;
if (page_sz > 0) if (page_sz > 0)
@ -312,7 +323,7 @@ fu_chunk_array_new (const guint8 *data,
} }
/** /**
* fu_chunk_array_new_from_bytes: (skip): * fu_chunk_array_new_from_bytes:
* @blob: a #GBytes * @blob: a #GBytes
* @addr_start: the hardware address offset, or 0 * @addr_start: the hardware address offset, or 0
* @page_sz: the hardware page size, or 0 * @page_sz: the hardware page size, or 0
@ -336,3 +347,13 @@ fu_chunk_array_new_from_bytes (GBytes *blob,
return fu_chunk_array_new (data, (guint32) sz, return fu_chunk_array_new (data, (guint32) sz,
addr_start, page_sz, packet_sz); addr_start, page_sz, packet_sz);
} }
static void
fu_chunk_class_init (FuChunkClass *klass)
{
}
static void
fu_chunk_init (FuChunk *self)
{
}

View File

@ -6,15 +6,11 @@
#pragma once #pragma once
#include <glib.h> #include <glib-object.h>
typedef struct { #define FU_TYPE_CHUNK (fu_chunk_get_type ())
guint32 idx;
guint32 page; G_DECLARE_FINAL_TYPE (FuChunk, fu_chunk, FU, CHUNK, GObject)
guint32 address;
const guint8 *data;
guint32 data_sz;
} FuChunk;
guint32 fu_chunk_get_idx (FuChunk *self); guint32 fu_chunk_get_idx (FuChunk *self);
guint32 fu_chunk_get_page (FuChunk *self); guint32 fu_chunk_get_page (FuChunk *self);
@ -29,7 +25,7 @@ FuChunk *fu_chunk_new (guint32 idx,
guint32 address, guint32 address,
const guint8 *data, const guint8 *data,
guint32 data_sz); guint32 data_sz);
gchar *fu_chunk_to_string (FuChunk *item); gchar *fu_chunk_to_string (FuChunk *self);
gchar *fu_chunk_array_to_string (GPtrArray *chunks); gchar *fu_chunk_array_to_string (GPtrArray *chunks);
GPtrArray *fu_chunk_array_new (const guint8 *data, GPtrArray *fu_chunk_array_new (const guint8 *data,

View File

@ -731,6 +731,7 @@ LIBFWUPDPLUGIN_1.5.6 {
fu_chunk_get_data_sz; fu_chunk_get_data_sz;
fu_chunk_get_idx; fu_chunk_get_idx;
fu_chunk_get_page; fu_chunk_get_page;
fu_chunk_get_type;
fu_common_get_memory_size; fu_common_get_memory_size;
fu_common_strjoin_array; fu_common_strjoin_array;
fu_common_uri_get_scheme; fu_common_uri_get_scheme;

View File

@ -222,6 +222,7 @@ if get_option('introspection') and get_option('gusb')
'LIBFWUPDPLUGIN', 'LIBFWUPDPLUGIN',
'@INPUT@', '@INPUT@',
'@OUTPUT@', '@OUTPUT@',
'--override', 'fu_chunk_get_type', '1.5.6',
], ],
) )