Blame SOURCES/0058-fpi-ssm-Add-possibility-to-jump-to-a-state-or-next-o.patch

73b847
From d35cadd5fd81067bfc5bf6f5595b98d4227ad190 Mon Sep 17 00:00:00 2001
73b847
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
73b847
Date: Fri, 22 Nov 2019 17:19:27 +0100
73b847
Subject: [PATCH 058/181] fpi-ssm: Add possibility to jump to a state (or next
73b847
 one) with delay
73b847
73b847
This allows to have an automatic cleanup of the timeout source when the
73b847
the callback is reached and to avoid to do further state changes in the
73b847
middle.
73b847
---
73b847
 doc/libfprint-sections.txt |   3 +
73b847
 libfprint/fpi-ssm.c        | 118 +++++++++++++++++++++++++++++++++++++
73b847
 libfprint/fpi-ssm.h        |   6 ++
73b847
 3 files changed, 127 insertions(+)
73b847
73b847
diff --git a/doc/libfprint-sections.txt b/doc/libfprint-sections.txt
73b847
index 0abe584..9fb01bd 100644
73b847
--- a/doc/libfprint-sections.txt
73b847
+++ b/doc/libfprint-sections.txt
73b847
@@ -215,7 +215,10 @@ fpi_ssm_free
73b847
 fpi_ssm_start
73b847
 fpi_ssm_start_subsm
73b847
 fpi_ssm_next_state
73b847
+fpi_ssm_next_state_delayed
73b847
 fpi_ssm_jump_to_state
73b847
+fpi_ssm_jump_to_state_delayed
73b847
+fpi_ssm_cancel_delayed_state_change
73b847
 fpi_ssm_mark_completed
73b847
 fpi_ssm_mark_failed
73b847
 fpi_ssm_set_data
73b847
diff --git a/libfprint/fpi-ssm.c b/libfprint/fpi-ssm.c
73b847
index 5299e2d..38186d2 100644
73b847
--- a/libfprint/fpi-ssm.c
73b847
+++ b/libfprint/fpi-ssm.c
73b847
@@ -87,6 +87,7 @@ struct _FpiSsm
73b847
   int                     nr_states;
73b847
   int                     cur_state;
73b847
   gboolean                completed;
73b847
+  GSource                *timeout;
73b847
   GError                 *error;
73b847
   FpiSsmCompletedCallback callback;
73b847
   FpiSsmHandlerCallback   handler;
73b847
@@ -170,6 +171,7 @@ fpi_ssm_free (FpiSsm *machine)
73b847
   if (machine->ssm_data_destroy)
73b847
     g_clear_pointer (&machine->ssm_data, machine->ssm_data_destroy);
73b847
   g_clear_pointer (&machine->error, g_error_free);
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
   g_free (machine);
73b847
 }
73b847
 
73b847
@@ -231,7 +233,9 @@ __subsm_complete (FpiSsm *ssm, FpDevice *_dev, GError *error)
73b847
 void
73b847
 fpi_ssm_start_subsm (FpiSsm *parent, FpiSsm *child)
73b847
 {
73b847
+  BUG_ON (parent->timeout);
73b847
   child->parentsm = parent;
73b847
+  g_clear_pointer (&parent->timeout, g_source_destroy);
73b847
   fpi_ssm_start (child, __subsm_complete);
73b847
 }
73b847
 
73b847
@@ -246,7 +250,12 @@ void
73b847
 fpi_ssm_mark_completed (FpiSsm *machine)
73b847
 {
73b847
   BUG_ON (machine->completed);
73b847
+  BUG_ON (machine->timeout);
73b847
+  BUG_ON (machine->timeout != NULL);
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
   machine->completed = TRUE;
73b847
+
73b847
   if (machine->error)
73b847
     fp_dbg ("%p completed with error: %s", machine, machine->error->message);
73b847
   else
73b847
@@ -297,6 +306,10 @@ fpi_ssm_next_state (FpiSsm *machine)
73b847
   g_return_if_fail (machine != NULL);
73b847
 
73b847
   BUG_ON (machine->completed);
73b847
+  BUG_ON (machine->timeout != NULL);
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
+
73b847
   machine->cur_state++;
73b847
   if (machine->cur_state == machine->nr_states)
73b847
     fpi_ssm_mark_completed (machine);
73b847
@@ -304,6 +317,56 @@ fpi_ssm_next_state (FpiSsm *machine)
73b847
     __ssm_call_handler (machine);
73b847
 }
73b847
 
73b847
+void
73b847
+fpi_ssm_cancel_delayed_state_change (FpiSsm *machine)
73b847
+{
73b847
+  g_return_if_fail (machine);
73b847
+  BUG_ON (machine->completed);
73b847
+  BUG_ON (machine->timeout == NULL);
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
+}
73b847
+
73b847
+static void
73b847
+on_device_timeout_next_state (FpDevice *dev,
73b847
+                              gpointer  user_data)
73b847
+{
73b847
+  FpiSsm *machine = user_data;
73b847
+
73b847
+  machine->timeout = NULL;
73b847
+  fpi_ssm_next_state (machine);
73b847
+}
73b847
+
73b847
+/**
73b847
+ * fpi_ssm_next_state_delayed:
73b847
+ * @machine: an #FpiSsm state machine
73b847
+ * @delay: the milliseconds to wait before switching to the next state
73b847
+ *
73b847
+ * Iterate to next state of a state machine with a delay of @delay ms. If the
73b847
+ * current state is the last state, then the state machine will be marked as
73b847
+ * completed, as if calling fpi_ssm_mark_completed().
73b847
+ */
73b847
+void
73b847
+fpi_ssm_next_state_delayed (FpiSsm *machine,
73b847
+                            int     delay)
73b847
+{
73b847
+  g_autofree char *source_name = NULL;
73b847
+
73b847
+  g_return_if_fail (machine != NULL);
73b847
+  BUG_ON (machine->completed);
73b847
+  BUG_ON (machine->timeout != NULL);
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
+  machine->timeout = fpi_device_add_timeout (machine->dev, delay,
73b847
+                                             on_device_timeout_next_state,
73b847
+                                             machine);
73b847
+
73b847
+  source_name = g_strdup_printf ("[%s] ssm %p jump to next state %d",
73b847
+                                 fp_device_get_device_id (machine->dev),
73b847
+                                 machine, machine->cur_state + 1);
73b847
+  g_source_set_name (machine->timeout, source_name);
73b847
+}
73b847
+
73b847
 /**
73b847
  * fpi_ssm_jump_to_state:
73b847
  * @machine: an #FpiSsm state machine
73b847
@@ -318,10 +381,65 @@ fpi_ssm_jump_to_state (FpiSsm *machine, int state)
73b847
 {
73b847
   BUG_ON (machine->completed);
73b847
   BUG_ON (state < 0 || state >= machine->nr_states);
73b847
+  BUG_ON (machine->timeout != NULL);
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
   machine->cur_state = state;
73b847
   __ssm_call_handler (machine);
73b847
 }
73b847
 
73b847
+typedef struct
73b847
+{
73b847
+  FpiSsm *machine;
73b847
+  int     next_state;
73b847
+} FpiSsmJumpToStateDelayedData;
73b847
+
73b847
+static void
73b847
+on_device_timeout_jump_to_state (FpDevice *dev,
73b847
+                                 gpointer  user_data)
73b847
+{
73b847
+  FpiSsmJumpToStateDelayedData *data = user_data;
73b847
+
73b847
+  data->machine->timeout = NULL;
73b847
+  fpi_ssm_jump_to_state (data->machine, data->next_state);
73b847
+}
73b847
+
73b847
+/**
73b847
+ * fpi_ssm_jump_to_state_delayed:
73b847
+ * @machine: an #FpiSsm state machine
73b847
+ * @state: the state to jump to
73b847
+ * @delay: the milliseconds to wait before switching to @state state
73b847
+ *
73b847
+ * Jump to the @state state with a delay of @delay milliseconds, bypassing
73b847
+ * intermediary states.
73b847
+ */
73b847
+void
73b847
+fpi_ssm_jump_to_state_delayed (FpiSsm *machine,
73b847
+                               int     state,
73b847
+                               int     delay)
73b847
+{
73b847
+  FpiSsmJumpToStateDelayedData *data;
73b847
+  g_autofree char *source_name = NULL;
73b847
+
73b847
+  g_return_if_fail (machine != NULL);
73b847
+  BUG_ON (machine->completed);
73b847
+  BUG_ON (machine->timeout != NULL);
73b847
+
73b847
+  data = g_new0 (FpiSsmJumpToStateDelayedData, 1);
73b847
+  data->machine = machine;
73b847
+  data->next_state = state;
73b847
+
73b847
+  g_clear_pointer (&machine->timeout, g_source_destroy);
73b847
+  machine->timeout = fpi_device_add_timeout_full (machine->dev, delay,
73b847
+                                                  on_device_timeout_jump_to_state,
73b847
+                                                  data, g_free);
73b847
+
73b847
+  source_name = g_strdup_printf ("[%s] ssm %p jump to state %d",
73b847
+                                 fp_device_get_device_id (machine->dev),
73b847
+                                 machine, state);
73b847
+  g_source_set_name (machine->timeout, source_name);
73b847
+}
73b847
+
73b847
 /**
73b847
  * fpi_ssm_get_cur_state:
73b847
  * @machine: an #FpiSsm state machine
73b847
diff --git a/libfprint/fpi-ssm.h b/libfprint/fpi-ssm.h
73b847
index 57e7d10..05e6cf0 100644
73b847
--- a/libfprint/fpi-ssm.h
73b847
+++ b/libfprint/fpi-ssm.h
73b847
@@ -73,6 +73,12 @@ void fpi_ssm_start_subsm (FpiSsm *parent,
73b847
 void fpi_ssm_next_state (FpiSsm *machine);
73b847
 void fpi_ssm_jump_to_state (FpiSsm *machine,
73b847
                             int     state);
73b847
+void fpi_ssm_next_state_delayed (FpiSsm *machine,
73b847
+                                 int     delay);
73b847
+void fpi_ssm_jump_to_state_delayed (FpiSsm *machine,
73b847
+                                    int     state,
73b847
+                                    int     delay);
73b847
+void fpi_ssm_cancel_delayed_state_change (FpiSsm *machine);
73b847
 void fpi_ssm_mark_completed (FpiSsm *machine);
73b847
 void fpi_ssm_mark_failed (FpiSsm *machine,
73b847
                           GError *error);
73b847
-- 
73b847
2.24.1
73b847