commit 8841dfec1ad670fc584fd5d47c1f157108888976
parent e70905eea22e5bd5dd21607d73eeaaf3547aa677
Author: Christian Grothoff <christian@grothoff.org>
Date: Tue, 10 Dec 2019 15:02:55 +0100
add option to enable sold-out detection
Diffstat:
| M | src/main.c | | | 53 | ++++++++++++++++++++++++++++++++++++++++++++++++++--- |
1 file changed, 50 insertions(+), 3 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -249,9 +249,16 @@ struct Product
unsigned long long number;
/**
+ * Set to #GNUNET_YES if this product was found
+ * to have been sold out (VEND failure).
+ */
+ int sold_out;
+
+ /**
* Key for the product (optional, needed to test the application without vending machine)
*/
char key;
+
};
@@ -271,6 +278,11 @@ struct PaymentActivity
struct TALER_MERCHANT_CheckPaymentOperation *cpo;
/**
+ * The product being sold.
+ */
+ struct Product *product;
+
+ /**
* Order ID for pending order
*/
char *order_id;
@@ -572,6 +584,11 @@ static int disable_mdb;
static int disable_tty;
/**
+ * Global option '-s' to enable sold-out detection.
+ */
+static int sold_out_enabled;
+
+/**
* Taler wallet application identifier
*/
static const uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 };
@@ -1278,7 +1295,7 @@ proposal_cb (void *cls,
* @return payment activity for the order, NULL on failure
*/
static struct PaymentActivity *
-launch_payment (const struct Product *product)
+launch_payment (struct Product *product)
{
struct PaymentActivity *pa;
json_t *orderReq;
@@ -1318,6 +1335,7 @@ launch_payment (const struct Product *product)
return NULL;
}
pa = GNUNET_new (struct PaymentActivity);
+ pa->product = product;
pa->amount = product->price;
pa->po = TALER_MERCHANT_order_put (ctx,
backendBaseUrl,
@@ -1369,6 +1387,7 @@ refund_complete_cb (void *cls,
{
struct Refund *r = cls;
+ (void) obj;
r->rio = NULL;
if (MHD_HTTP_OK != http_status)
{
@@ -1390,6 +1409,7 @@ refund_complete_cb (void *cls,
static void
vend_failure ()
{
+ struct Product *p;
struct Refund *r;
if (NULL == payment_activity)
@@ -1397,8 +1417,11 @@ vend_failure ()
GNUNET_break (0);
return;
}
+ p = payment_activity->product;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Received MDB vend failure, refunding customer\n");
+ "Received MDB vend failure for `%s', refunding customer\n",
+ p->description);
+ p->sold_out = GNUNET_YES;
mdb.cmd = &endSession;
mdb.session_running = GNUNET_NO;
r = GNUNET_new (struct Refund);
@@ -1512,6 +1535,17 @@ read_keyboard_command (void *cls)
for (unsigned int i = 0; i < products_length; i++)
if (((char) input) == products[i].key)
{
+ if ( (sold_out_enabled) &&
+ (products[i].sold_out) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Product %s sold out, denying vend\n",
+ products[i].description);
+ mdb.cmd = &denyVend;
+ run_mdb_event_loop ();
+ start_read_keyboard ();
+ return;
+ }
payment_activity = launch_payment (&products[i]);
start_read_keyboard ();
return;
@@ -1659,7 +1693,7 @@ write_mdb_command (void *cls)
chkSum += mdb.txBuffer[idx] = mdb.cmd->cmd.bin[idx];
for (size_t idx = 0; idx < mdb.cmd->data.bin_size; idx++)
chkSum += mdb.txBuffer[idx + mdb.cmd->cmd.bin_size] =
- mdb.cmd->data.bin[idx];
+ mdb.cmd->data.bin[idx];
mdb.txBuffer[mdb.cmd->cmd.bin_size + mdb.cmd->data.bin_size] =
(uint8_t) (chkSum & 0xFF);
}
@@ -1783,6 +1817,15 @@ handle_command (const char *hex,
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Product %u selected on NFC\n",
product);
+ if ( (sold_out_enabled) &&
+ (products[i].sold_out) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Product %s sold out, denying vend\n",
+ products[i].description);
+ mdb.cmd = &denyVend;
+ return;
+ }
payment_activity = launch_payment (&products[i]);
return;
}
@@ -2513,6 +2556,10 @@ main (int argc,
"disable-mdb",
"disable all interactions with the MDB (for testing without machine)",
&disable_mdb),
+ GNUNET_GETOPT_option_flag ('s',
+ "enable-soldout",
+ "enable detection of sold-out products, preventing vend operations of the respective product until the process is restarted",
+ &sold_out_enabled),
GNUNET_GETOPT_option_flag ('t',
"disable-tty",
"disable all keyboard interactions (for running from systemd)",