diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..504c2dd --- /dev/null +++ b/src/main.c @@ -0,0 +1,166 @@ +/* + This file is part of TALER + Copyright (C) 2019 GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + + You should have received a copy of the GNU General Public License +along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +*/ + +/** +* @file main.c +* @brief main functionality of the application +* @author BOSS Marco +* @author ... +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h> +#include <unistd.h> +#include <nfc/nfc.h> +#include <curl/curl.h> + +#include "nfc-wallet/nfc.h" +#include "taler-processing/communication.h" +#include "taler-processing/product.h" + +const char *CURRENCY = "KUDOS"; + + +ProductOrder product; +nfc_context *context = NULL; + + +void *start_nfc_transmission( void *ignored ) +{ + // supress warning about unused variable + (void)ignored; + + if( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL ) != 0 ){ + printf( "Error setting thread cancelling type\n"); + } + // start endless transmission loop (until threads gets cancelled) + while( 1 ){ + if( pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0 ){ + printf("Error setting thread cancelling state\n" ); + } + nfc_transmit( context, product.payUrl, strlen(product.payUrl ) ); + if( pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0){ + printf("Error setting thread cancelling state\n" ); + } + sleep(1); + } + return EXIT_SUCCESS; +} + + +int main( ) +{ + // initialize nfc + nfc_init( &context ); + if( !context ){ + printf( "Unable to init libnfc\n" ); + return EXIT_FAILURE; + } + + // inizialize taler + CURL *curl = NULL; + if( taler_init( &curl ) ){ + printf( "Unable to init taler communication\n" ); + return EXIT_FAILURE; + } + + // inizialize product + if( product_init( &product, CURRENCY ) ){ + printf( "Unable to init product\n" ); + return EXIT_FAILURE; + } + + + while( true ) + { + printf( "Waiting for MBD input\n" ); + printf( "Enter to simulate Snickers, x to quit\n"); + if( getchar() == 'x' ) + break; + + // DEMO snickers + product.amount = "0.1"; + product.product = "Snickers"; + + + // create the order request + taler_create_order_req( &product ); + + // create the order + while( taler_create_order( curl, &product ) ); + + // store the order id into the struct + product_set_order_id( &product ); + + // store the url to check wheter the payment happened or not + product_set_check_url( &product ); + + // check the payment status for the first time + while( taler_check_payment_status( curl, &product ) ); + + // store the url to pay, to do this task the payment status has to be called before, because the url will be in the response + product_set_pay_url( &product ); + + // start a thread to send payment request to taler wallet + pthread_t nfcThread; + if( pthread_create(&nfcThread, NULL, start_nfc_transmission, NULL) ){ + printf( "Could not create thread" ); + return EXIT_FAILURE; + } + + // check the payment status, if paid exit while loop and end thread transmitting nfc messages + while( !product.paid ){ + printf( "Order payment processing\n" ); + fflush(stdout); + sleep(5); + while( taler_check_payment_status( curl, &product ) ); + // set the boolean paid member of ProductOrder struct + product_set_paid_status( &product ); + printf( "Payment status paid: %s\n\n", (product.paid ? "true" : "false") ); + } + printf( "Order no.: %s paid!\n\n", product.orderID ); + + // send cancel request to nfc thread + while( pthread_cancel(nfcThread) != 0 ){ + printf( "Error sending cancel request to thread for nfc transmission" ); + } + void*res; + if( pthread_join( nfcThread, &res ) == 0 ){ + printf( "Thread for nfc transmission finished\n" ); + fflush(stdout); + }else if( res == PTHREAD_CANCELED ){ + printf( "Thread for nfc transmission finished\n" ); + fflush(stdout); + } + + // reset the product + product_reset( &product ); + + } + + // clear all initialized data + nfc_exit( context ); + product_exit( &product ); + taler_exit( &curl ); + + return EXIT_SUCCESS; + +} |