gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taler-mdb] 07/23: refactor


From: gnunet
Subject: [taler-taler-mdb] 07/23: refactor
Date: Wed, 04 Dec 2019 14:15:53 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository taler-mdb.

commit fbef2d25b3d2e161b35226d2c878762101c8b23f
Author: Christian Grothoff <address@hidden>
AuthorDate: Wed Dec 4 12:35:29 2019 +0100

    refactor
---
 src/main.c | 246 ++++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 139 insertions(+), 107 deletions(-)

diff --git a/src/main.c b/src/main.c
index a1dac68..d6c283e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -68,6 +68,11 @@ along with
 
 #define NFC_FAILURE_RETRY_FREQ GNUNET_TIME_UNIT_SECONDS
 
+/**
+ * How long do we wait at most for an ACK from MDB?
+ */
+#define MAX_ACK_LATENCY GNUNET_TIME_UNIT_SECONDS
+
 /**
  * Timeout in milliseconds for libnfc operations.
  */
@@ -118,45 +123,23 @@ along with
 #define READER_REVALUE_DENIED "0E"
 
 
-/**
- * @brief FRAMEBUFFER_DEVICE framebuffer device to diplay qr code
- */
-static const char *FRAMEBUFFER_DEVICE = "/dev/fb1";
-
-static const char *UART_DEVICE = "/dev/ttyAMA0";
-
-/**
-  * Taler wallet application identifier
-  */
-static const uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 
};
-
-/**
-  * NFC select file command to select wallet aid
-  */
-static const uint8_t select_file[] = { 0x00, 0xA4, 0x04, 0x00, 0x07 };
-/**
-  * NFC put command to send data to the wallet
-  */
-static const uint8_t put_data[] = { 0x00, 0xDA, 0x01, 0x00, 0x7c, 0x01 };
-
-#if FUTURE_FEATURES
-/* tunneling */
-static const uint8_t get_data[] = { 0x00, 0xCA, 0x01, 0x00, 0x00, 0x00 };
-#endif
-
-
 struct MdbBlock
 {
   uint8_t*bin;
+
   size_t bin_size;
 };
 
 
-/* Datatype for mdb command */
-struct mdbCmd
+/**
+ * Datatype for mdb command
+ */
+struct MdbCommand
 {
   const char *name;
+
   struct MdbBlock cmd;
+
   struct MdbBlock data;
 };
 
@@ -240,24 +223,24 @@ struct PaymentActivity
   int wallet_has_uri;
 };
 
-#define MAX_ACK_LATENCY GNUNET_TIME_UNIT_SECONDS
 
-struct mdbHandle
-{
-  int uartfd;
-  struct mdbCmd *cmd;
-  struct mdbCmd *last_cmd;
-  int session_running;
 
-  struct GNUNET_TIME_Absolute ack_timeout;
+struct MdbHandle
+{
 
   uint8_t rxBuffer[MAX_SIZE_RX_BUFFER];
 
-  size_t rx_off;
+  uint8_t txBuffer[MAX_SIZE_TX_BUFFER];
 
   struct GNUNET_SCHEDULER_Task *rtask;
 
-  uint8_t txBuffer[MAX_SIZE_TX_BUFFER];
+  struct GNUNET_SCHEDULER_Task *wtask;
+
+  const struct MdbCommand *cmd;
+
+  const struct MdbCommand *last_cmd;
+
+  size_t rx_off;
 
   /**
    * Current write offset in @e txBuffer.
@@ -270,9 +253,14 @@ struct mdbHandle
    */
   size_t tx_len;
 
-  struct GNUNET_SCHEDULER_Task *wtask;
+  struct GNUNET_TIME_Absolute ack_timeout;
 
   struct termios uart_opts_backup;
+
+  int session_running;
+
+  int uartfd;
+
 };
 
 
@@ -312,6 +300,7 @@ struct Display
   struct fb_fix_screeninfo fix_info;
 };
 
+
 /**
  * NFC context used by the NFC reader
  */
@@ -382,14 +371,43 @@ static struct Product *products;
  */
 static unsigned int products_length;
 
-static struct mdbHandle mdb;
+/**
+ * Data associated with the MDB session.
+ */
+static struct MdbHandle mdb;
+
+static struct MdbCommand readerConfigData;
+static struct MdbCommand beginSession;
+static struct MdbCommand denyVend;
+static struct MdbCommand approveVend;
+static struct MdbCommand endSession;
+
+/**
+ * @brief FRAMEBUFFER_DEVICE framebuffer device to diplay qr code
+ */
+static const char *FRAMEBUFFER_DEVICE = "/dev/fb1";
+
+static const char *UART_DEVICE = "/dev/ttyAMA0";
+
+/**
+ * Taler wallet application identifier
+ */
+static const uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 
};
+
+/**
+ * NFC select file command to select wallet aid
+ */
+static const uint8_t select_file[] = { 0x00, 0xA4, 0x04, 0x00, 0x07 };
 
-struct mdbCmd readerConfigData;
-struct mdbCmd beginSession;
-struct mdbCmd denyVend;
-struct mdbCmd approveVend;
-struct mdbCmd endSession;
+/**
+ * NFC put command to send data to the wallet
+ */
+static const uint8_t put_data[] = { 0x00, 0xDA, 0x01, 0x00, 0x7c, 0x01 };
 
+#if FUTURE_FEATURES
+/* tunneling */
+static const uint8_t get_data[] = { 0x00, 0xCA, 0x01, 0x00, 0x00, 0x00 };
+#endif
 
 /**
  * Handle for the framebuffer device
@@ -492,48 +510,7 @@ show_qrcode (const char *uri)
 
 
 static void
-parse_block (struct MdbBlock *blk,
-             const char *hex)
-{
-  if (NULL == hex)
-  {
-    blk->bin_size = 0;
-    blk->bin = NULL;
-    return;
-  }
-  blk->bin_size = strlen (hex) / 2;
-  blk->bin = GNUNET_malloc (blk->bin_size);
-  for (size_t idx = 0; idx < blk->bin_size; idx++)
-  {
-    unsigned int val;
-
-    GNUNET_assert (1 ==
-                   sscanf (&hex[idx * 2],
-                           "%2X",
-                           &val));
-    blk->bin[idx] = (uint8_t) val;
-  }
-}
-
-
-struct mdbCmd
-setup_mdb_cmd (const char *name,
-               const char *cmd,
-               const char *data)
-{
-  struct mdbCmd cmdNew;
-
-  cmdNew.name = (NULL == name)
-                ? "No Cmd Name Set"
-                : name;
-  parse_block (&cmdNew.cmd, cmd);
-  parse_block (&cmdNew.data, data);
-  return cmdNew;
-}
-
-
-static void
-start_mdb (void);
+run_mdb_event_loop (void);
 
 
 
@@ -602,7 +579,8 @@ mdb_shutdown ()
     (void) close (mdb.uartfd);
     mdb.uartfd = -1;
   }
-
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Shutdown complete (including MDB)\n");
 }
 
 
@@ -636,7 +614,7 @@ shutdown_task (void *cls)
   /* last ditch saying nicely goodbye to MDB */
   in_shutdown = GNUNET_YES;
   mdb.cmd = &endSession;
-  start_mdb ();
+  run_mdb_event_loop ();
   if (NULL != ctx)
   {
     GNUNET_CURL_fini (ctx);
@@ -678,7 +656,7 @@ shutdown_task (void *cls)
                        0);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Shutdown complete\n");
+              "Shutdown complete (except for MDB)\n");
 }
 
 
@@ -693,6 +671,7 @@ connect_target (void *cls);
 static void
 wallet_select_aid (void *cls);
 
+
 /**
  * Transmit the pay uri from taler to the wallet application via NFC
  * @param cls closure
@@ -745,6 +724,7 @@ wallet_transmit_uri (void *cls)
                                                  pa);
 }
 
+
 /**
  * Select the taler wallet app via NFC
  * @param cls closure
@@ -804,8 +784,10 @@ wallet_select_aid (void *cls)
                                            pa);
 }
 
+
 /**
  * Connect the NFC reader with a compatible NFC target
+ *
  * @param cls closure
  */
 static void
@@ -850,8 +832,10 @@ connect_target (void *cls)
                                            pa);
 }
 
+
 /**
  * Open the NFC reader.
+ *
  * @param cls closure
  */
 static void
@@ -917,10 +901,10 @@ check_payment_cb (void *cls,
                   const char *taler_pay_uri)
 {
   struct PaymentActivity *pa = cls;
+
   (void) refunded;
   (void) refund_amount;
   (void) obj;
-
   pa->cpo = NULL;
   if (MHD_HTTP_OK != http_status)
   {
@@ -928,7 +912,7 @@ check_payment_cb (void *cls,
                 "Backend request to /check-payment failed: %u",
                 http_status);
     mdb.cmd = &denyVend;
-    start_mdb ();
+    run_mdb_event_loop ();
     cleanup_payment (pa);
     GNUNET_assert (payment_activity == pa);
     payment_activity = NULL;
@@ -972,8 +956,10 @@ check_payment_cb (void *cls,
   }
 }
 
+
 /**
  * Check the payment status again
+ *
  * @param cls closure
  */
 static void
@@ -992,8 +978,10 @@ check_payment_again (void *cls)
                                           pa);
 }
 
+
 /**
  * Callback for a PUT /order request
+ *
  * @param cls closure
  * @param http_status HTTP status code for this request
  * @param ec Taler error code
@@ -1018,7 +1006,7 @@ proposal_cb (void *cls,
                 http_status,
                 (int) ec);
     mdb.cmd = &denyVend;
-    start_mdb ();
+    run_mdb_event_loop ();
     cleanup_payment (pa);
     GNUNET_assert (payment_activity == pa);
     payment_activity = NULL;
@@ -1035,11 +1023,12 @@ proposal_cb (void *cls,
                                           GNUNET_TIME_UNIT_ZERO,
                                           &check_payment_cb,
                                           pa);
-
 }
 
+
 /**
  * Launch a new order
+ *
  * @param product information for product to sell
  * @return payment activity for the order, NULL on failure
  */
@@ -1104,6 +1093,7 @@ start_read_keyboard (void);
 
 /**
  * Read the character from stdin and activate the selected task
+ *
  * @param cls closure
  */
 static void
@@ -1125,7 +1115,7 @@ read_keyboard_command (void *cls)
     if (NULL != payment_activity)
     {
       mdb.cmd = &denyVend;
-      start_mdb ();
+      run_mdb_event_loop ();
       cleanup_payment (payment_activity);
       payment_activity = NULL;
     }
@@ -1157,6 +1147,7 @@ read_keyboard_command (void *cls)
   start_read_keyboard ();
 }
 
+
 /**
  * Wait for a keyboard input
  */
@@ -1209,7 +1200,7 @@ write_mdb_command (void *cls)
   /* ongoing write incomplete, continue later */
   if (mdb.tx_off < mdb.tx_len)
   {
-    start_mdb ();
+    run_mdb_event_loop ();
     return;
   }
   if (NULL != mdb.last_cmd)
@@ -1221,7 +1212,7 @@ write_mdb_command (void *cls)
     if (0 != del.rel_value_us)
     {
       if (did_write)
-        start_mdb ();
+        run_mdb_event_loop ();
       else
         mdb.wtask = GNUNET_SCHEDULER_add_delayed (del,
                                                   &write_mdb_command,
@@ -1256,7 +1247,7 @@ write_mdb_command (void *cls)
   }
   mdb.last_cmd = mdb.cmd;
   mdb.cmd = NULL;
-  start_mdb ();
+  run_mdb_event_loop ();
 }
 
 
@@ -1495,9 +1486,7 @@ read_mdb_command (void *cls)
         break;
     if (cmdEndIdx == mdb.rx_off)
     {
-      /* check we have more buffer space available,
-   otherwise the rxBuffer was fundamentally too
-   small to receive an entire command! */
+      /* check to make sure rxBuffer was big enough in principle */
       if ( (cmdStartIdx > 0) ||
            (mdb.rx_off < sizeof (mdb.rxBuffer)) )
       {
@@ -1520,12 +1509,12 @@ read_mdb_command (void *cls)
     mdb.rx_off -= (cmdEndIdx + 1);
   }
 
-  start_mdb ();
+  run_mdb_event_loop ();
 }
 
 
 static void
-start_mdb ()
+run_mdb_event_loop ()
 {
   struct GNUNET_DISK_FileHandle fh = { mdb.uartfd };
 
@@ -1684,7 +1673,7 @@ mdb_init ()
                          "tcsetattr");
     return GNUNET_SYSERR;
   }
-  start_mdb ();
+  run_mdb_event_loop ();
   return GNUNET_OK;
 }
 
@@ -1896,6 +1885,47 @@ run (void *cls,
 }
 
 
+static void
+parse_block (struct MdbBlock *blk,
+             const char *hex)
+{
+  if (NULL == hex)
+  {
+    blk->bin_size = 0;
+    blk->bin = NULL;
+    return;
+  }
+  blk->bin_size = strlen (hex) / 2;
+  blk->bin = GNUNET_malloc (blk->bin_size);
+  for (size_t idx = 0; idx < blk->bin_size; idx++)
+  {
+    unsigned int val;
+
+    GNUNET_assert (1 ==
+                   sscanf (&hex[idx * 2],
+                           "%2X",
+                           &val));
+    blk->bin[idx] = (uint8_t) val;
+  }
+}
+
+
+static struct MdbCommand
+setup_mdb_cmd (const char *name,
+               const char *cmd,
+               const char *data)
+{
+  struct MdbCommand cmdNew;
+
+  cmdNew.name = (NULL == name)
+                ? "No Cmd Name Set"
+                : name;
+  parse_block (&cmdNew.cmd, cmd);
+  parse_block (&cmdNew.data, data);
+  return cmdNew;
+}
+
+
 int
 main (int argc,
       char*const*argv)
@@ -1936,8 +1966,10 @@ main (int argc,
                             NULL);
   if (have_tty)
   {
-    // Restore previous TTY settings
-    if (0 != tcsetattr (STDIN_FILENO, TCSANOW, &tty_opts_backup))
+    /* Restore previous TTY settings */
+    if (0 != tcsetattr (STDIN_FILENO,
+                        TCSANOW,
+                        &tty_opts_backup))
       fprintf (stderr,
                "Failed to restore terminal discipline\n");
   }

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]