fpi-log: Add functions to dump bytes

Drivers may need to to hex-dumps for debugging purposes, so expose what
we already had in the fpi transfers in the internal API
This commit is contained in:
Marco Trevisan (Treviño)
2026-06-22 15:19:30 +02:00
parent 5cadc90e7a
commit 3db8184a30
6 changed files with 111 additions and 42 deletions
+2
View File
@@ -348,6 +348,8 @@ fpi_image_device_set_bz3_threshold
<SECTION> <SECTION>
<FILE>fpi-log</FILE> <FILE>fpi-log</FILE>
fp_dbg fp_dbg
fp_dbg_hex_dump_bytes
fp_dbg_hex_dump_data
fp_info fp_info
fp_warn fp_warn
fp_err fp_err
+74
View File
@@ -0,0 +1,74 @@
/*
* FpiLog - Internal logging functions
* Copyright (C) 2020 Benjamin Berg <bberg@redhat.com>
* Copyright (C) 2025 Joshua Grisham <josh@joshuagrisham.com>
* Copyright (C) 2026 Marco Trevisan (Treviño) <mail@3v1n0.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fpi-log.h"
/**
* fpi_dbg_hex_dump_data:
* @buf: Bytes buffer to dump
* @len: Length of @buf to dump
*
* Prints hex dump of @buf to fp_dbg()
*/
void
(fpi_dbg_hex_dump_data) (const gchar *log_domain,
const guint8 *buf,
gsize len)
{
g_autoptr(GString) line = NULL;
if (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, log_domain))
return;
if (G_UNLIKELY (len == 0 || !buf))
return;
line = g_string_new ("");
for (gint i = 0; i < len; i++)
{
g_string_append_printf (line, "%02x ", buf[i]);
if ((i + 1) % 16 == 0)
{
g_log (log_domain, G_LOG_LEVEL_DEBUG, "%s", line->str);
g_string_set_size (line, 0);
}
}
if (line->len)
g_log (log_domain, G_LOG_LEVEL_DEBUG, "%s", line->str);
}
/**
* fpi_dbg_hex_dump_bytes:
* @bytes: #GBytes to dump
*
* Prints hex dump of @bytes to fp_dbg()
*/
void
(fpi_dbg_hex_dump_bytes) (const gchar *log_domain,
GBytes *bytes)
{
gsize length = 0;
const guint8 *data = g_bytes_get_data (bytes, &length);
(fpi_dbg_hex_dump_data) (log_domain, data, length);
}
+28
View File
@@ -1,6 +1,7 @@
/* /*
* Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org> * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
* Copyright (C) 2018 Bastien Nocera <hadess@hadess.net> * Copyright (C) 2018 Bastien Nocera <hadess@hadess.net>
* Copyright (C) 2026 Marco Trevisan (Treviño) <mail@3v1n0.net>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@@ -96,3 +97,30 @@
* Same as BUG_ON() but is always true. * Same as BUG_ON() but is always true.
*/ */
#define BUG() BUG_ON (1) #define BUG() BUG_ON (1)
void (fpi_dbg_hex_dump_data) (const gchar *log_domain,
const guint8 *buf,
gsize len);
/**
* fp_dbg_hex_dump_data:
* @buf: Bytes buffer to dump
* @len: Length of @buf to dump
*
* Prints hex dump of @buf to fp_dbg()
*/
#define fp_dbg_hex_dump_data(buf, len) \
(fpi_dbg_hex_dump_data) (G_LOG_DOMAIN, (buf), (len))
void (fpi_dbg_hex_dump_bytes) (const gchar *log_domain,
GBytes *bytes);
/**
* fp_dbg_hex_dump_bytes:
* @bytes: #GBytes to dump
*
* Prints hex dump of @bytes to fp_dbg()
*/
#define fp_dbg_hex_dump_bytes(bytes) \
(fpi_dbg_hex_dump_bytes) \
(G_LOG_DOMAIN, (bytes))
+3 -23
View File
@@ -18,6 +18,7 @@
*/ */
#include "fpi-spi-transfer.h" #include "fpi-spi-transfer.h"
#include "fpi-log.h"
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <linux/spi/spidev.h> #include <linux/spi/spidev.h>
#include <errno.h> #include <errno.h>
@@ -48,27 +49,6 @@ static gsize block_size = 0;
G_DEFINE_BOXED_TYPE (FpiSpiTransfer, fpi_spi_transfer, fpi_spi_transfer_ref, fpi_spi_transfer_unref) G_DEFINE_BOXED_TYPE (FpiSpiTransfer, fpi_spi_transfer, fpi_spi_transfer_ref, fpi_spi_transfer_unref)
static void
dump_buffer (guchar *buf, gssize dump_len)
{
g_autoptr(GString) line = NULL;
line = g_string_new ("");
/* Dump the buffer. */
for (gssize i = 0; i < dump_len; i++)
{
g_string_append_printf (line, "%02x ", buf[i]);
if ((i + 1) % 16 == 0)
{
g_debug ("%s", line->str);
g_string_set_size (line, 0);
}
}
if (line->len)
g_debug ("%s", line->str);
}
static void static void
log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error) log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error)
{ {
@@ -82,7 +62,7 @@ log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error)
transfer->length_rd); transfer->length_rd);
if (transfer->buffer_wr) if (transfer->buffer_wr)
dump_buffer (transfer->buffer_wr, transfer->length_wr); fp_dbg_hex_dump_data (transfer->buffer_wr, transfer->length_wr);
} }
else else
{ {
@@ -98,7 +78,7 @@ log_transfer (FpiSpiTransfer *transfer, gboolean submit, GError *error)
transfer->length_wr, transfer->length_wr,
transfer->length_rd); transfer->length_rd);
if (transfer->buffer_rd) if (transfer->buffer_rd)
dump_buffer (transfer->buffer_rd, transfer->length_rd); fp_dbg_hex_dump_data (transfer->buffer_rd, transfer->length_rd);
} }
} }
} }
+3 -19
View File
@@ -65,25 +65,9 @@ log_transfer (FpiUsbTransfer *transfer, gboolean submit, GError *error)
if (!submit == !!(transfer->endpoint & FPI_USB_ENDPOINT_IN)) if (!submit == !!(transfer->endpoint & FPI_USB_ENDPOINT_IN))
{ {
g_autoptr(GString) line = NULL; fp_dbg_hex_dump_data (transfer->buffer,
gssize dump_len; (transfer->endpoint & FPI_USB_ENDPOINT_IN) ?
transfer->actual_length : transfer->length);
dump_len = (transfer->endpoint & FPI_USB_ENDPOINT_IN) ? transfer->actual_length : transfer->length;
line = g_string_new ("");
/* Dump the buffer. */
for (gint i = 0; i < dump_len; i++)
{
g_string_append_printf (line, "%02x ", transfer->buffer[i]);
if ((i + 1) % 16 == 0)
{
g_debug ("%s", line->str);
g_string_set_size (line, 0);
}
}
if (line->len)
g_debug ("%s", line->str);
} }
} }
} }
+1
View File
@@ -21,6 +21,7 @@ libfprint_private_sources = [
'fpi-device.c', 'fpi-device.c',
'fpi-image-device.c', 'fpi-image-device.c',
'fpi-image.c', 'fpi-image.c',
'fpi-log.c',
'fpi-print.c', 'fpi-print.c',
'fpi-ssm.c', 'fpi-ssm.c',
'fpi-usb-transfer.c', 'fpi-usb-transfer.c',