mirror of
https://gitlab.freedesktop.org/libfprint/libfprint.git
synced 2026-09-10 21:06:51 +00:00
fpi-device: Introduce a properly defined fpi_device_emulation_mode_enabled()
Drivers may do some adjustments in test mode, initially we wanted to reduce this to the minimum but the usage of `FP_DEVICE_EMULATION` exploded, so move this instead into a properly defined variable that: - Uses GLib compiler optimizations - Is read just once per process - Cannot be misused
This commit is contained in:
@@ -282,6 +282,7 @@ fpi_device_get_current_action
|
||||
fpi_device_retry_new
|
||||
fpi_device_error_new
|
||||
fpi_device_retry_new_msg
|
||||
fpi_device_emulation_mode_enabled
|
||||
fpi_device_error_new_msg
|
||||
fpi_device_get_driver_data
|
||||
fpi_device_get_enroll_data
|
||||
|
||||
@@ -1567,7 +1567,7 @@ egis_etu905_probe (FpDevice *device)
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (device))
|
||||
serial = g_strdup ("emulated-device");
|
||||
else
|
||||
serial = g_usb_device_get_string_descriptor (usb_dev,
|
||||
|
||||
@@ -1500,7 +1500,7 @@ egismoc_probe (FpDevice *device)
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (device)))
|
||||
serial = g_strdup ("emulated-device");
|
||||
else
|
||||
serial = g_usb_device_get_string_descriptor (usb_dev,
|
||||
|
||||
@@ -557,7 +557,7 @@ capture_run_state (FpiSsm *ssm, FpDevice *dev)
|
||||
else
|
||||
{
|
||||
/* XXX: The timeout is emulated incorrectly, resulting in a zero byte read. */
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
fpi_ssm_mark_completed (ssm);
|
||||
else
|
||||
fpi_ssm_mark_failed (ssm, fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
|
||||
|
||||
@@ -94,7 +94,7 @@ static void
|
||||
elanspi_do_hwreset (FpiDeviceElanSpi *self, GError **err)
|
||||
{
|
||||
/* Skip in emulation mode, since we don't mock hid devices */
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
return;
|
||||
|
||||
/*
|
||||
@@ -485,7 +485,8 @@ elanspi_capture_old_handler (FpiSsm *ssm, FpDevice *dev)
|
||||
if (!(self->sensor_status & 4))
|
||||
{
|
||||
/* has the timeout expired? -- disabled in testing since valgrind is very slow */
|
||||
if (g_get_monotonic_time () > self->capture_timeout && g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") != 0)
|
||||
if (g_get_monotonic_time () > self->capture_timeout &&
|
||||
!fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
{
|
||||
/* end with a timeout */
|
||||
fpi_ssm_mark_failed (ssm, g_error_new (G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "timed out waiting for new line"));
|
||||
|
||||
@@ -970,7 +970,7 @@ fp_enroll_sm_run_state (FpiSsm *ssm, FpDevice *device)
|
||||
user_id_len = MIN (100, user_id_len);
|
||||
finger = 1;
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
memset (self->template_id, 0, TEMPLATE_ID_SIZE);
|
||||
uid = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
|
||||
user_id,
|
||||
@@ -1390,7 +1390,7 @@ gx_fp_probe (FpDevice *device)
|
||||
if (!g_usb_device_claim_interface (usb_dev, 0, 0, &error))
|
||||
goto err_close;
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
{
|
||||
|
||||
serial = g_strdup ("emulated-device");
|
||||
|
||||
@@ -2317,7 +2317,7 @@ mafp_probe (FpDevice *device)
|
||||
if (!g_usb_device_claim_interface (usb_dev, self->interface_num, 0, &error))
|
||||
goto err_close;
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (device))
|
||||
{
|
||||
serial = g_strdup ("emulated-device");
|
||||
}
|
||||
@@ -2378,7 +2378,7 @@ mafp_init (FpDevice *device)
|
||||
else
|
||||
fp_dbg ("device no storage");
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (device))
|
||||
{
|
||||
serial = g_strdup ("emulated-device");
|
||||
}
|
||||
|
||||
@@ -855,7 +855,7 @@ secugen_frame_chunk_cb (FpiUsbTransfer *transfer,
|
||||
|
||||
/* Outside emulation a complete sensor frame is expected. */
|
||||
if (self->bulk_offset < SECUGEN_RAW_SIZE &&
|
||||
g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") != 0)
|
||||
fpi_device_emulation_mode_enabled (dev))
|
||||
{
|
||||
fp_warn ("Short image data: got %" G_GSIZE_FORMAT ", expected %d",
|
||||
self->bulk_offset, SECUGEN_RAW_SIZE);
|
||||
@@ -2225,7 +2225,7 @@ dev_change_state (FpImageDevice *dev,
|
||||
* starts with the finger already present, so report it directly -
|
||||
* the same approach other drivers use for replay-incompatible
|
||||
* hardware behavior (e.g. elanspi's HID reset skip). */
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
{
|
||||
fpi_image_device_report_finger_status (dev, TRUE);
|
||||
break;
|
||||
|
||||
@@ -1211,7 +1211,7 @@ prob_msg_cb (FpiDeviceSynaptics *self,
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (self)))
|
||||
serial = g_strdup ("emulated-device");
|
||||
else
|
||||
serial = g_usb_device_get_string_descriptor (usb_dev,
|
||||
|
||||
@@ -1380,7 +1380,7 @@ dev_init (FpImageDevice *dev)
|
||||
|
||||
g_clear_pointer (&self->rand, g_rand_free);
|
||||
self->rand = g_rand_new ();
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (FP_DEVICE (dev)))
|
||||
g_rand_set_seed (self->rand, 0xFACADE);
|
||||
|
||||
driver_data = fpi_device_get_driver_data (FP_DEVICE (dev));
|
||||
|
||||
@@ -119,7 +119,7 @@ async_abort_callback (FpiUsbTransfer *transfer, FpDevice *device,
|
||||
|
||||
/* In normal case endpoint is empty */
|
||||
if (g_error_matches (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_TIMED_OUT) ||
|
||||
(g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0 && transfer->actual_length == 0))
|
||||
(fpi_device_emulation_mode_enabled (device) && transfer->actual_length == 0))
|
||||
{
|
||||
g_clear_error (&error);
|
||||
fpi_ssm_next_state (transfer->ssm);
|
||||
|
||||
@@ -50,6 +50,35 @@ fp_device_get_instance_private (FpDevice *self)
|
||||
g_type_class_get_instance_private_offset (dev_class));
|
||||
}
|
||||
|
||||
/**
|
||||
* fpi_device_emulation_mode_enabled:
|
||||
* @device: The #FpDevice to check
|
||||
*
|
||||
* Checks if the device is running in emulation mode, which is enabled by
|
||||
* setting the FP_DEVICE_EMULATION environment variable to a '1' value.
|
||||
* This is used by some drivers to enable special behavior for testing
|
||||
* and development purposes.
|
||||
*/
|
||||
gboolean
|
||||
(fpi_device_emulation_mode_enabled) (FpDevice *device)
|
||||
{
|
||||
static gsize emulation_mode = 0;
|
||||
|
||||
/* FIXME: Add some build constraints to make sure this cannot ever be enabled
|
||||
* in production, outside in the installed tests case.
|
||||
*/
|
||||
|
||||
/* This is a global value for now, but ideally we may support a different
|
||||
* value per device.
|
||||
*/
|
||||
if (g_once_init_enter (&emulation_mode))
|
||||
g_once_init_leave (&emulation_mode,
|
||||
g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0 ?
|
||||
TRUE : G_MAXSIZE);
|
||||
|
||||
return emulation_mode == TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* fpi_device_class_auto_initialize_features:
|
||||
* @device_class: An #FpDeviceClass to initialize
|
||||
|
||||
@@ -332,4 +332,11 @@ gboolean fpi_device_report_finger_status_changes (FpDevice *device,
|
||||
FpFingerStatusFlags added_status,
|
||||
FpFingerStatusFlags removed_status);
|
||||
|
||||
/* Debugging utilities */
|
||||
|
||||
#define fpi_device_emulation_mode_enabled(dev) \
|
||||
G_UNLIKELY ((fpi_device_emulation_mode_enabled) ((dev)))
|
||||
|
||||
gboolean (fpi_device_emulation_mode_enabled) (FpDevice *device);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
@@ -295,7 +295,7 @@ fpi_print_generate_user_id (FpPrint *print)
|
||||
if (!username)
|
||||
username = "nobody";
|
||||
|
||||
if (g_strcmp0 (g_getenv ("FP_DEVICE_EMULATION"), "1") == 0)
|
||||
if (fpi_device_emulation_mode_enabled (NULL))
|
||||
rand_id = 0;
|
||||
else
|
||||
rand_id = g_random_int ();
|
||||
|
||||
Reference in New Issue
Block a user