taler-mdb

GNU Taler Extensions and Integrations
Log | Files | Refs | Submodules | README | LICENSE

commit 69c67e844b79461db05e184edc4d96310e75cd93
parent f75c128255bfae841066440d5763ef176beddc79
Author: Christian Grothoff <christian@grothoff.org>
Date:   Fri,  6 Dec 2019 19:10:58 +0100

add -t flag to disable tty

Diffstat:
Msrc/main.c | 128+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 76 insertions(+), 52 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -557,6 +557,11 @@ static char *uart_device_filename; static int disable_mdb; /** + * Global option '-t' to disable stdin / terminal. + */ +static int disable_tty; + +/** * Taler wallet application identifier */ static const uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 }; @@ -752,6 +757,7 @@ cleanup_payment (struct PaymentActivity *pa) GNUNET_free (pa); } + /** * @brief Shutdown the mdb communication tasks */ @@ -1135,7 +1141,7 @@ check_payment_cb (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Backend request to /check-payment failed: %u\n", http_status); - mdb.cmd = &denyVend; + mdb.cmd = &denyVend; run_mdb_event_loop (); cleanup_payment (pa); GNUNET_assert (payment_activity == pa); @@ -1148,7 +1154,7 @@ check_payment_cb (void *cls, mdb.cmd = &approveVend; payment_activity->paid = GNUNET_YES; run_mdb_event_loop (); - if (disable_mdb) + if ((disable_mdb) && (! disable_tty)) { GNUNET_SCHEDULER_cancel (keyboard_task); keyboard_task = NULL; @@ -1166,8 +1172,8 @@ check_payment_cb (void *cls, (NULL == pa->delay_pay_task) ) { pa->delay_pay_task = GNUNET_SCHEDULER_add_delayed (MAX_HTTP_RETRY_FREQ, - &check_payment_again, - pa); + &check_payment_again, + pa); } } if (NULL == pa->taler_pay_uri) @@ -1292,7 +1298,7 @@ launch_payment (const struct Product *product) "amount", TALER_JSON_from_amount (&product->price), "fulfillment_url", fulflmntUrl // ,"auto_refund", GNUNET_JSON_from_time_rel (MAX_REFUND_DELAY) - ); + ); GNUNET_free (fulflmntUrl); if (NULL == orderReq) { @@ -1540,6 +1546,7 @@ start_read_keyboard () NULL); } + /** * @brief Send data to the vmc via the uart bus * @@ -1563,7 +1570,7 @@ write_mdb_command (void *cls) run_mdb_event_loop (); return; } - /* if command was sent completely send the rest */ + /* if command was sent completely send the rest */ if (mdb.tx_off < mdb.tx_len) { ssize_t ret = write (mdb.uartfd, @@ -1577,14 +1584,14 @@ write_mdb_command (void *cls) "write", uart_device_filename); if (in_shutdown) - mdb_shutdown (); + mdb_shutdown (); else - GNUNET_SCHEDULER_shutdown (); + GNUNET_SCHEDULER_shutdown (); return; } mdb.tx_off += ret; - /* if command was sent sucessfully start the timer for ACK timeout */ - if ( (ret > 0) && + /* if command was sent sucessfully start the timer for ACK timeout */ + if ( (ret > 0) && (mdb.tx_off == mdb.tx_len) ) mdb.ack_timeout = GNUNET_TIME_relative_to_absolute (MAX_ACK_LATENCY); } @@ -1682,7 +1689,7 @@ handle_command (const char *hex, { unsigned int cmd; - /* if the received command is 0 or not a multiple of 2 we cannot parse it */ + /* if the received command is 0 or not a multiple of 2 we cannot parse it */ if (0 == hex_len) return; if (0 != (hex_len % 2)) @@ -1694,7 +1701,7 @@ handle_command (const char *hex, GNUNET_break_op (0); return; } - /* convert the received 2 bytes from ASCII to hex */ + /* convert the received 2 bytes from ASCII to hex */ if (1 != sscanf (hex, "%2X", &cmd)) @@ -1706,7 +1713,7 @@ handle_command (const char *hex, GNUNET_break_op (0); return; } - /* parse the first byte (cmd) and the second byte (subcmd) */ + /* parse the first byte (cmd) and the second byte (subcmd) */ switch (cmd) { case VMC_VEND: @@ -1832,9 +1839,9 @@ handle_command (const char *hex, mdb.session_running = GNUNET_NO; break; case VMC_READER_CANCEL: - mdb.cmd = &readerCancelled; - mdb.session_running = GNUNET_NO; - break; + mdb.cmd = &readerCancelled; + mdb.session_running = GNUNET_NO; + break; default: GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unknown MDB sub-command %X of command %X\n", @@ -1881,10 +1888,11 @@ handle_command (const char *hex, } } + /** * @brief Read in the sent commands of the VMC controller * - * @param cls closure + * @param cls closure */ static void read_mdb_command (void *cls) @@ -1894,7 +1902,7 @@ read_mdb_command (void *cls) size_t cmdEndIdx; (void) cls; - /* don't read if the mdb bus is disabled (only for testing) */ + /* don't read if the mdb bus is disabled (only for testing) */ GNUNET_assert (! disable_mdb); mdb.rtask = NULL; ret = read (mdb.uartfd, @@ -1936,14 +1944,14 @@ read_mdb_command (void *cls) GNUNET_SCHEDULER_shutdown (); return; } - /* move cmd in buffer to the beginning of the buffer */ + /* move cmd in buffer to the beginning of the buffer */ memmove (mdb.rxBuffer, &mdb.rxBuffer[cmdStartIdx], mdb.rx_off - cmdStartIdx); mdb.rx_off -= cmdStartIdx; break; } - /* if the full command was received parse it */ + /* if the full command was received parse it */ handle_command ((const char *) &mdb.rxBuffer[cmdStartIdx + 1], cmdEndIdx - cmdStartIdx - 1); /* move the data after the processed command to the left */ @@ -1960,6 +1968,7 @@ read_mdb_command (void *cls) run_mdb_event_loop (); } + /** * @brief Mdb event loop to start read and write tasks */ @@ -1968,19 +1977,19 @@ run_mdb_event_loop () { struct GNUNET_DISK_FileHandle fh = { mdb.uartfd }; - /* begin session if no cmd waits for sending and no cmd is received from the VMC */ + /* begin session if no cmd waits for sending and no cmd is received from the VMC */ if ( (GNUNET_NO == mdb.session_running) && (NULL == mdb.cmd) && (NULL == mdb.last_cmd) ) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Begining MDB session\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Begining MDB session\n"); mdb.cmd = &beginSession; } - /* start write task if he doesn't exist and if there is a cmd waiting to get sent */ + /* start write task if he doesn't exist and if there is a cmd waiting to get sent */ if ( (NULL == mdb.wtask) && ( (NULL != mdb.cmd) || - (in_shutdown) || + (in_shutdown) || (mdb.tx_len > mdb.tx_off) ) ) { if (disable_mdb) @@ -2000,7 +2009,7 @@ run_mdb_event_loop () mdb.last_cmd->name); handle_ack (); } - /* start read task if he doesn't exist and the mdb communication is not disabled (only for testing) */ + /* start read task if he doesn't exist and the mdb communication is not disabled (only for testing) */ if ( (NULL == mdb.rtask) && (! disable_mdb) ) mdb.rtask = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, @@ -2091,6 +2100,7 @@ read_products (void *cls, tmpProduct); } + /** * @brief Initialise the uart device to send mdb commands */ @@ -2102,7 +2112,7 @@ mdb_init () if (disable_mdb) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Running with MDB disabled!\n"); + "Running with MDB disabled!\n"); run_mdb_event_loop (); return GNUNET_OK; } @@ -2397,9 +2407,11 @@ run (void *cls, framebuffer_device_filename); } #endif - start_read_keyboard (); + if (! disable_tty) + start_read_keyboard (); } + /** * @brief Convert the ASCII cmd in @hex to hex and store it in the mdb data struct @blk * @@ -2430,13 +2442,14 @@ parse_block (struct MdbBlock *blk, } } + /** * @brief Create a new mdb command * - * @param *name pointer to the string containing the command name + * @param *name pointer to the string containing the command name * @param *cmd pointer to the string containing the command * @param *data pointer to the string containing the command data - * @return structure of type MdbCommand holding the given information by the parameters + * @return structure of type MdbCommand holding the given information by the parameters */ static struct MdbCommand setup_mdb_cmd (const char *name, @@ -2466,27 +2479,36 @@ main (int argc, "disable-mdb", "disable all interactions with the MDB (for testing without machine)", &disable_mdb), + GNUNET_GETOPT_option_flag ('t', + "disable-tty", + "disable all keyboard interactions (for running from systemd)", + &disable_tty), GNUNET_GETOPT_OPTION_END }; int have_tty; - have_tty = isatty (STDIN_FILENO); - if (have_tty) + if (! disable_tty) { - if (0 != tcgetattr (STDIN_FILENO, &tty_opts_backup)) - fprintf (stderr, - "Failed to get terminal discipline\n"); - tty_opts_raw = tty_opts_backup; - tty_opts_raw.c_lflag &= ~(ECHO | ECHONL | ICANON); - if (0 != tcsetattr (STDIN_FILENO, TCSANOW, &tty_opts_raw)) - fprintf (stderr, - "Failed to set terminal discipline\n"); + have_tty = isatty (STDIN_FILENO); + if (have_tty) + { + if (0 != tcgetattr (STDIN_FILENO, &tty_opts_backup)) + fprintf (stderr, + "Failed to get terminal discipline\n"); + tty_opts_raw = tty_opts_backup; + tty_opts_raw.c_lflag &= ~(ECHO | ECHONL | ICANON); + if (0 != tcsetattr (STDIN_FILENO, TCSANOW, &tty_opts_raw)) + fprintf (stderr, + "Failed to set terminal discipline\n"); + } } - /* make the needed commands for the communication with the vending machine controller */ readerConfigData = setup_mdb_cmd ("Reader Config", READER_CONFIG, - READER_FEATURE_LEVEL READER_COUNTRYCODE READER_SCALE_FACTOR READER_DECIMAL_PLACES READER_MAX_RESPONSE_TIME READER_MISC_OPTIONS); + READER_FEATURE_LEVEL READER_COUNTRYCODE + READER_SCALE_FACTOR READER_DECIMAL_PLACES + READER_MAX_RESPONSE_TIME + READER_MISC_OPTIONS); beginSession = setup_mdb_cmd ("Begin Session", READER_BEGIN_SESSION, READER_FUNDS_AVAILABLE); @@ -2494,8 +2516,8 @@ main (int argc, READER_VEND_APPROVE, READER_VEND_AMOUNT); readerCancelled = setup_mdb_cmd ("Confirm cancellation", - READER_CANCELLED, - NULL); + READER_CANCELLED, + NULL); denyVend = setup_mdb_cmd ("Deny Vend", READER_VEND_DENIED, NULL); @@ -2510,16 +2532,18 @@ main (int argc, options, &run, NULL); - if (have_tty) + if (! disable_tty) { - /* Restore previous TTY settings */ - if (0 != tcsetattr (STDIN_FILENO, - TCSANOW, - &tty_opts_backup)) - fprintf (stderr, - "Failed to restore terminal discipline\n"); + if (have_tty) + { + /* Restore previous TTY settings */ + if (0 != tcsetattr (STDIN_FILENO, + TCSANOW, + &tty_opts_backup)) + fprintf (stderr, + "Failed to restore terminal discipline\n"); + } } - if (GNUNET_OK != ret) return 1; return global_ret;