taler-mdb

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

commit 77ae5db261a31aef4f91f82fcd9a3cbfdb6f712c
parent 6fc962ebd7442e7d53ee11659c401c040c4d1cda
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed, 30 Oct 2019 17:45:00 +0100

initial indenting

Diffstat:
Msrc/communication.c | 320+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/communication.h | 15++++++++-------
Msrc/configuration.h | 32++++++++++++++++----------------
Msrc/main.c | 229++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/nfc.c | 140+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/nfc.h | 7++++---
Msrc/product.c | 148++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/product.h | 42++++++++++++++++++++++--------------------
Msrc/wallet.c | 153++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/wallet.h | 11++++++-----
10 files changed, 582 insertions(+), 515 deletions(-)

diff --git a/src/communication.c b/src/communication.c @@ -27,194 +27,208 @@ along with #include <stdbool.h> #include "communication.h" -#include "nfc-wallet/wallet.h" +#include "wallet.h" #include "configuration.h" -int taler_init(CURL **curl) +int taler_init (CURL **curl) { - if( *curl != NULL ){ - printf( "taler_init: curl handle already initialized\n" ); - return EXIT_FAILURE; - } - - // initialize curl - CURLcode result = curl_global_init( CURL_GLOBAL_ALL ); - *curl = curl_easy_init(); - if( !(*curl) || result != CURLE_OK ){ - printf( "taler_init: could not inizialize curl handle\n" ); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + if ( *curl != NULL ) + { + printf ("taler_init: curl handle already initialized\n"); + return EXIT_FAILURE; + } + + // initialize curl + CURLcode result = curl_global_init (CURL_GLOBAL_ALL); + *curl = curl_easy_init (); + if ( ! (*curl) ||(result != CURLE_OK) ) + { + printf ("taler_init: could not inizialize curl handle\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; } -void taler_exit( CURL **curl ) +void taler_exit (CURL **curl) { - curl_easy_cleanup(*curl); - curl_global_cleanup(); + curl_easy_cleanup (*curl); + curl_global_cleanup (); } -int taler_alloc_string( char **string, size_t size ) +int taler_alloc_string (char **string, size_t size) { - *string = malloc( size ); - if( !(*string) ){ - printf( "taler_alloc: unable to allocate string" ); - exit( EXIT_FAILURE ); - } - return EXIT_SUCCESS; + *string = malloc (size); + if ( ! (*string) ) + { + printf ("taler_alloc: unable to allocate string"); + exit (EXIT_FAILURE); + } + return EXIT_SUCCESS; } -static size_t _taler_write_response( void *contents, size_t size, size_t nmemb, void *userp ) +static size_t _taler_write_response (void *contents, size_t size, size_t nmemb, + void *userp) { - size_t realSize = size * nmemb; - struct TalerResponse *response = (struct TalerResponse *)userp; - - char *tempString = realloc( response->string, response->size + realSize + 1 ); - if( !tempString ) { - printf( "Allocation failure" ); - return EXIT_FAILURE; - } - - response->string = tempString; - memcpy(&( response->string[response->size] ), contents, realSize ); - response->size += realSize; - response->string[response->size] = 0; - - return realSize; + size_t realSize = size * nmemb; + struct TalerResponse *response = (struct TalerResponse *) userp; + + char *tempString = realloc (response->string, response->size + realSize + 1); + if ( ! tempString ) + { + printf ("Allocation failure"); + return EXIT_FAILURE; + } + + response->string = tempString; + memcpy (&(response->string[response->size]), contents, realSize); + response->size += realSize; + response->string[response->size] = 0; + + return realSize; } -int taler_create_order_req( ProductOrder *product ) +int taler_create_order_req (ProductOrder *product) { - char buffer[256]; + char buffer[256]; - sprintf( buffer, JSON_ORDER, product->product, product->currency, product->amount, product->product ); + sprintf (buffer, JSON_ORDER, product->product, product->currency, + product->amount, product->product); - char* temp = realloc( product->orderRequest, strlen(buffer) + 1 ); - if( !temp ){ - printf( "could not allocate order\n" ); - return EXIT_FAILURE; - } + char*temp = realloc (product->orderRequest, strlen (buffer) + 1); + if ( ! temp ) + { + printf ("could not allocate order\n"); + return EXIT_FAILURE; + } - product->orderRequest = temp; - strcpy( product->orderRequest, buffer ); - printf( "Created order Request: \n%s\n\n", product->orderRequest ); + product->orderRequest = temp; + strcpy (product->orderRequest, buffer); + printf ("Created order Request: \n%s\n\n", product->orderRequest); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } -int taler_create_order( CURL *curl, ProductOrder *product ) +int taler_create_order (CURL *curl, ProductOrder *product) { - // reset the response size - product->response->size = 0; - - // set the url - char url[ strlen(BACKEND_BASE_URL) + strlen( ORDER ) + 1 ]; - sprintf( url, "%s%s", BACKEND_BASE_URL, ORDER ); - curl_easy_setopt( curl, CURLOPT_URL, url ); - - // set the authentication headers - struct curl_slist *list = NULL; - list = curl_slist_append( list, AUTH_HEADER ); - curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list ); - - // curl option "post" - curl_easy_setopt( curl, CURLOPT_HTTPPOST, true ); - curl_easy_setopt( curl, CURLOPT_POSTFIELDS, product->orderRequest ); - - // pass the write function and the struct to write to - curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response ); - curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response ); - - // perform the call - CURLcode result = curl_easy_perform( curl ); - if( result != CURLE_OK ){ - printf( "could not communicate with \"%s\"\n", url ); - return EXIT_FAILURE; - } - - // reset the curl options - curl_easy_reset( curl ); - - // clean up - curl_slist_free_all( list ); - - return EXIT_SUCCESS; + // reset the response size + product->response->size = 0; + + // set the url + char url[ strlen (BACKEND_BASE_URL) + strlen (ORDER) + 1 ]; + sprintf (url, "%s%s", BACKEND_BASE_URL, ORDER); + curl_easy_setopt (curl, CURLOPT_URL, url); + + // set the authentication headers + struct curl_slist *list = NULL; + list = curl_slist_append (list, AUTH_HEADER); + curl_easy_setopt (curl, CURLOPT_HTTPHEADER, list); + + // curl option "post" + curl_easy_setopt (curl, CURLOPT_HTTPPOST, true); + curl_easy_setopt (curl, CURLOPT_POSTFIELDS, product->orderRequest); + + // pass the write function and the struct to write to + curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, _taler_write_response); + curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response); + + // perform the call + CURLcode result = curl_easy_perform (curl); + if ( result != CURLE_OK ) + { + printf ("could not communicate with \"%s\"\n", url); + return EXIT_FAILURE; + } + + // reset the curl options + curl_easy_reset (curl); + + // clean up + curl_slist_free_all (list); + + return EXIT_SUCCESS; } -int taler_check_payment_status( CURL *curl, ProductOrder *product ) +int taler_check_payment_status (CURL *curl, ProductOrder *product) { - // reset the response size - product->response->size = 0; + // reset the response size + product->response->size = 0; - // set the url - curl_easy_setopt( curl, CURLOPT_URL, product->checkUrl ); + // set the url + curl_easy_setopt (curl, CURLOPT_URL, product->checkUrl); - // set the authentication headers - struct curl_slist *list = NULL; - list = curl_slist_append( list, AUTH_HEADER ); - curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list ); + // set the authentication headers + struct curl_slist *list = NULL; + list = curl_slist_append (list, AUTH_HEADER); + curl_easy_setopt (curl, CURLOPT_HTTPHEADER, list); - // curl option "get" - curl_easy_setopt( curl, CURLOPT_HTTPGET, true ); + // curl option "get" + curl_easy_setopt (curl, CURLOPT_HTTPGET, true); - // pass the write function and the struct to write to - curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response ); - curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response ); + // pass the write function and the struct to write to + curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, _taler_write_response); + curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response); - // perform the call - CURLcode result = curl_easy_perform(curl); - if( result != CURLE_OK ){ - printf( "could not communicate with \"%s\"\n", product->checkUrl ); - return EXIT_FAILURE; - } + // perform the call + CURLcode result = curl_easy_perform (curl); + if ( result != CURLE_OK ) + { + printf ("could not communicate with \"%s\"\n", product->checkUrl); + return EXIT_FAILURE; + } - // reset the curl options - curl_easy_reset( curl ); + // reset the curl options + curl_easy_reset (curl); - // clean up - curl_slist_free_all( list ); + // clean up + curl_slist_free_all (list); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } -int taler_parse_json( const TalerResponse *response, const char *memberName, char **returnStr, bool isBoolean ) +int taler_parse_json (const TalerResponse *response, const char *memberName, + char **returnStr, bool isBoolean) { - if( !(*returnStr) ) - taler_alloc_string( returnStr, 1 ); - char *result = NULL; - char *temp = NULL; - char mbr[64]; - - if( isBoolean ){ - // If the wanted member is of type boolean - sprintf( mbr, "\"%s\": true", memberName ); - if( (temp = strstr( response->string, mbr)) != NULL ) - result = "true"; - else - result = "false"; - }else{ - // String type members - sprintf( mbr, "\"%s\":", memberName ); - if( (temp = strstr( response->string, mbr )) != NULL ){ - if( (temp = strstr( response->string, ": ")) != NULL ){ - result = strtok( temp, "\""); - result = strtok( NULL, "\""); - } - } + if ( ! (*returnStr) ) + taler_alloc_string (returnStr, 1); + char *result = NULL; + char *temp = NULL; + char mbr[64]; + + if ( isBoolean ) + { + // If the wanted member is of type boolean + sprintf (mbr, "\"%s\": true", memberName); + if ( (temp = strstr (response->string, mbr)) != NULL ) + result = "true"; + else + result = "false"; + } + else{ + // String type members + sprintf (mbr, "\"%s\":", memberName); + if ( (temp = strstr (response->string, mbr)) != NULL ) + { + if ( (temp = strstr (response->string, ": ")) != NULL ) + { + result = strtok (temp, "\""); + result = strtok (NULL, "\""); + } } - if( !result ){ - printf( "taler_parse_json: no member named \"%s\" found!\n\n", memberName ); - return EXIT_FAILURE; - } - temp = realloc( *returnStr, strlen(result) + 1 ); - if( !temp ){ - printf( "taler_parse_json: could not allocate memory for member\n" ); - return EXIT_FAILURE; - } - *returnStr = temp; - strcpy( *returnStr, result ); - - return EXIT_SUCCESS; + } + if ( ! result ) + { + printf ("taler_parse_json: no member named \"%s\" found!\n\n", memberName); + return EXIT_FAILURE; + } + temp = realloc (*returnStr, strlen (result) + 1); + if ( ! temp ) + { + printf ("taler_parse_json: could not allocate memory for member\n"); + return EXIT_FAILURE; + } + *returnStr = temp; + strcpy (*returnStr, result); + + return EXIT_SUCCESS; } - - diff --git a/src/communication.h b/src/communication.h @@ -34,18 +34,19 @@ along with #include "product.h" -int taler_init( CURL **curl ); +int taler_init (CURL **curl); -void taler_exit( CURL **curl ); +void taler_exit (CURL **curl); -int taler_alloc_string( char **string, size_t size ); +int taler_alloc_string (char **string, size_t size); -int taler_create_order_req( ProductOrder *product ); +int taler_create_order_req (ProductOrder *product); -int taler_create_order( CURL *curl, ProductOrder *product ); +int taler_create_order (CURL *curl, ProductOrder *product); -int taler_check_payment_status( CURL *curl, ProductOrder *product ); +int taler_check_payment_status (CURL *curl, ProductOrder *product); -int taler_parse_json( const TalerResponse *response, const char *memberName, char **returnStr, bool isBoolean ); +int taler_parse_json (const TalerResponse *response, const char *memberName, + char **returnStr, bool isBoolean); #endif // COMM_H diff --git a/src/configuration.h b/src/configuration.h @@ -26,29 +26,29 @@ along with #ifndef URL_H #define URL_H -static const char* const ORDER = "/order"; -static const char* const CHECK = "/check-payment"; -static const char* const ORDER_CHECK = "?order_id="; +static const char*const ORDER = "/order"; +static const char*const CHECK = "/check-payment"; +static const char*const ORDER_CHECK = "?order_id="; -static const char* const BACKEND_BASE_URL = "https://backend.demo.taler.net"; +static const char*const BACKEND_BASE_URL = "https://backend.demo.taler.net"; -static const char* const AUTH_HEADER = "Authorization: ApiKey sandbox"; +static const char*const AUTH_HEADER = "Authorization: ApiKey sandbox"; -static const char* const JSON_PAID = "paid"; -static const char* const JSON_PAY_URI = "taler_pay_uri"; -static const char* const JSON_ORDER_ID = "order_id"; +static const char*const JSON_PAID = "paid"; +static const char*const JSON_PAY_URI = "taler_pay_uri"; +static const char*const JSON_ORDER_ID = "order_id"; //// will be replaced with libjansson functionalites ----------------------------------------------------------------------------------- // ajust sprintf in taler_create_order_req communication.c if changed -static const char* const JSON_ORDER = "{\n" - " \"order\":" - " {\n" - " \"summary\": \"%s\",\n" - " \"amount\": \"%s:%s\",\n" - " \"fulfillment_url\": \"taler://fulfillment-success/Enjoy+your+%s!\"\n" - " }\n" - "}"; +static const char*const JSON_ORDER = "{\n" + " \"order\":" + " {\n" + " \"summary\": \"%s\",\n" + " \"amount\": \"%s:%s\",\n" + " \"fulfillment_url\": \"taler://fulfillment-success/Enjoy+your+%s!\"\n" + " }\n" + "}"; #endif // URL_H diff --git a/src/main.c b/src/main.c @@ -32,9 +32,9 @@ along with #include <nfc/nfc.h> #include <curl/curl.h> -#include "nfc-wallet/nfc.h" -#include "taler-processing/communication.h" -#include "taler-processing/product.h" +#include "nfc.h" +#include "communication.h" +#include "product.h" const char *CURRENCY = "KUDOS"; @@ -43,124 +43,141 @@ ProductOrder product; nfc_context *context = NULL; -void *start_nfc_transmission( void *ignored ) +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"); + // 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"); } - // 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); + nfc_transmit (context, product.payUrl, strlen (product.payUrl) ); + if ( pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0) + { + printf ("Error setting thread cancelling state\n"); } - return EXIT_SUCCESS; + sleep (1); + } + return EXIT_SUCCESS; } -int main( ) +int main ( ) { - // initialize nfc - nfc_init( &context ); - if( !context ){ - printf( "Unable to init libnfc\n" ); - return EXIT_FAILURE; + // 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; } - // inizialize taler - CURL *curl = NULL; - if( taler_init( &curl ) ){ - printf( "Unable to init taler communication\n" ); - 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); - // inizialize product - if( product_init( &product, CURRENCY ) ){ - printf( "Unable to init product\n" ); - return EXIT_FAILURE; + // send cancel request to nfc thread + while ( pthread_cancel (nfcThread) != 0 ) + { + printf ("Error sending cancel request to thread for nfc transmission"); } - - - while( true ) + void*res; + if ( pthread_join (nfcThread, &res) == 0 ) { - 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 ); - + printf ("Thread for nfc transmission finished\n"); + fflush (stdout); + } + else if ( res == PTHREAD_CANCELED ) + { + printf ("Thread for nfc transmission finished\n"); + fflush (stdout); } - // clear all initialized data - nfc_exit( context ); - product_exit( &product ); - taler_exit( &curl ); + // reset the product + product_reset (&product); + + } + + // clear all initialized data + nfc_exit (context); + product_exit (&product); + taler_exit (&curl); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/src/nfc.c b/src/nfc.c @@ -35,84 +35,96 @@ along with #define UID_LEN_LOWER 4 -int nfc_transmit( nfc_context *context, const char *talerPayUrl, size_t urlSize ) -{ - nfc_device *pnd = NULL; - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// MEMORY LEAK? NFC OPEN? +int nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t urlSize) +{ + nfc_device *pnd = NULL; - pnd = nfc_open( context, NULL ); // NULL could be replaced with connstring if the correct is known - if( !pnd ){ - printf( "Unable to open NFC device\n" ); - nfc_close( pnd ); - return EXIT_FAILURE; - } + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// MEMORY LEAK? NFC OPEN? - // initialize device as reader - if( nfc_initiator_init( pnd ) < 0 ){ - nfc_perror( pnd, "nfc_initiator_init" ); - nfc_close( pnd ); - return EXIT_FAILURE; - } + pnd = nfc_open (context, NULL); // NULL could be replaced with connstring if the correct is known + if ( ! pnd ) + { + printf ("Unable to open NFC device\n"); + nfc_close (pnd); + return EXIT_FAILURE; + } - printf( "Device %s opened: '%s'\n\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd) ); + // initialize device as reader + if ( nfc_initiator_init (pnd) < 0 ) + { + nfc_perror (pnd, "nfc_initiator_init"); + nfc_close (pnd); + return EXIT_FAILURE; + } - nfc_target nt; + printf ("Device %s opened: '%s'\n\n", nfc_device_get_name (pnd), + nfc_device_get_connstring (pnd) ); - // connect to a target device - if( nfc_connect_target( pnd, &nt ) ){ - nfc_close( pnd ); - return EXIT_FAILURE; - }; + nfc_target nt; - // send the message to the wallet - if( wallet_transmit( pnd, talerPayUrl, urlSize ) ){ - // the transmition failed, the target has to be reselected when using MIFARE as defined in libnfc --> exit - nfc_close( pnd ); - return EXIT_FAILURE; - } + // connect to a target device + if ( nfc_connect_target (pnd, &nt) ) + { + nfc_close (pnd); + return EXIT_FAILURE; + } + ; + + // send the message to the wallet + if ( wallet_transmit (pnd, talerPayUrl, urlSize) ) + { + // the transmition failed, the target has to be reselected when using MIFARE as defined in libnfc --> exit + nfc_close (pnd); + return EXIT_FAILURE; + } - // clean up - nfc_initiator_deselect_target( pnd ); - nfc_close( pnd ); + // clean up + nfc_initiator_deselect_target (pnd); + nfc_close (pnd); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } -int nfc_connect_target( nfc_device *pnd, nfc_target *nt ) +int nfc_connect_target (nfc_device *pnd, nfc_target *nt) { - const nfc_modulation nmMifare[] = { { - .nmt = NMT_ISO14443A, - .nbr = NBR_106, - } }; - - printf( "nfc_connect_target: trying to connect to target\n" ); - int ctr = 2; - while( ctr > 0 ){ - // set uid lenght to zero ( in case of second selecting the length still has the old value ) - nt->nti.nai.szUidLen = 0; - if( nfc_initiator_select_passive_target( pnd, nmMifare[0], NULL, 0, nt ) <= 0 ) { - printf( "nfc_connect_target: failed to connect\n" ); - } else if( nt->nti.nai.szUidLen > UID_LEN_UPPER || nt->nti.nai.szUidLen < UID_LEN_LOWER ) { - printf( "nfc_connect_target: failed to connect\n" ); - } else { - printf( "nfc_connect_target: Target selected!\n" ); - nfc_display_target_uid( nt ); - return EXIT_SUCCESS; - } - sleep(1); - ctr--; + const nfc_modulation nmMifare[] = { { + .nmt = NMT_ISO14443A, + .nbr = NBR_106, + } }; + + printf ("nfc_connect_target: trying to connect to target\n"); + int ctr = 2; + while ( ctr > 0 ) + { + // set uid lenght to zero ( in case of second selecting the length still has the old value ) + nt->nti.nai.szUidLen = 0; + if ( nfc_initiator_select_passive_target (pnd, nmMifare[0], NULL, 0, nt) <= + 0 ) + { + printf ("nfc_connect_target: failed to connect\n"); + } + else if ((nt->nti.nai.szUidLen > UID_LEN_UPPER) ||(nt->nti.nai.szUidLen < + UID_LEN_LOWER) ) + { + printf ("nfc_connect_target: failed to connect\n"); + } + else { + printf ("nfc_connect_target: Target selected!\n"); + nfc_display_target_uid (nt); + return EXIT_SUCCESS; } + sleep (1); + ctr--; + } - return EXIT_FAILURE; + return EXIT_FAILURE; } -void nfc_display_target_uid( nfc_target *nt ) +void nfc_display_target_uid (nfc_target *nt) { - printf( "UID: " ); - for ( unsigned int uidPos = 0; uidPos < nt->nti.nai.szUidLen; uidPos++ ) { - printf( "%.2x ", nt->nti.nai.abtUid[uidPos] ); - } - printf("\n\n"); + printf ("UID: "); + for ( unsigned int uidPos = 0; uidPos < nt->nti.nai.szUidLen; uidPos++ ) { + printf ("%.2x ", nt->nti.nai.abtUid[uidPos]); + } + printf ("\n\n"); } - diff --git a/src/nfc.h b/src/nfc.h @@ -27,8 +27,9 @@ along with #include <stdio.h> #include <nfc/nfc.h> -int nfc_transmit( nfc_context *context, const char *talerPayUrl , size_t urlSize ); +int nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t + urlSize); -int nfc_connect_target( nfc_device *pnd, nfc_target *nt ); +int nfc_connect_target (nfc_device *pnd, nfc_target *nt); -void nfc_display_target_uid( nfc_target *nt ); +void nfc_display_target_uid (nfc_target *nt); diff --git a/src/product.c b/src/product.c @@ -29,98 +29,106 @@ the attributes. #include "product.h" #include "configuration.h" -#include "taler-processing/communication.h" +#include "communication.h" -void product_reset( ProductOrder *product ) +void product_reset (ProductOrder *product) { - product->amount = NULL; - product->product = NULL; - product->paid = false; + product->amount = NULL; + product->product = NULL; + product->paid = false; } -int product_init( ProductOrder *product, const char *currency ) +int product_init (ProductOrder *product, const char *currency) { - if( !product ){ - printf( "product_init: product order struct must be provided\n" ); - return EXIT_FAILURE; - } - - // malloc for every string member with size 1, every other func just calls realloc, because this struct gets used over and over again - product->response = malloc( sizeof(TalerResponse) ); - if( !product->response ){ - printf( "product_init: unable to allocate memory for response struct\n" ); - return EXIT_FAILURE; - } - if( taler_alloc_string( &(product->response->string), 1 ) ) - return EXIT_FAILURE; - if( taler_alloc_string( &(product->orderID), 1 ) ) - return EXIT_FAILURE; - if( taler_alloc_string( &(product->orderRequest), 1 ) ) - return EXIT_FAILURE; - if( taler_alloc_string( &(product->payUrl), 1 ) ) - return EXIT_FAILURE; - if( taler_alloc_string( &(product->checkUrl), 1 ) ) - return EXIT_FAILURE; - if( taler_alloc_string( &(product->currency), strlen(currency) + 1 ) ) - return EXIT_FAILURE; - strcpy( product->currency, currency ); - product->amount = NULL; - product->product = NULL; - product->paid = false; + if ( ! product ) + { + printf ("product_init: product order struct must be provided\n"); + return EXIT_FAILURE; + } - return EXIT_SUCCESS; + // malloc for every string member with size 1, every other func just calls realloc, because this struct gets used over and over again + product->response = malloc (sizeof(TalerResponse) ); + if ( ! product->response ) + { + printf ("product_init: unable to allocate memory for response struct\n"); + return EXIT_FAILURE; + } + if ( taler_alloc_string (&(product->response->string), 1) ) + return EXIT_FAILURE; + if ( taler_alloc_string (&(product->orderID), 1) ) + return EXIT_FAILURE; + if ( taler_alloc_string (&(product->orderRequest), 1) ) + return EXIT_FAILURE; + if ( taler_alloc_string (&(product->payUrl), 1) ) + return EXIT_FAILURE; + if ( taler_alloc_string (&(product->checkUrl), 1) ) + return EXIT_FAILURE; + if ( taler_alloc_string (&(product->currency), strlen (currency) + 1) ) + return EXIT_FAILURE; + strcpy (product->currency, currency); + product->amount = NULL; + product->product = NULL; + product->paid = false; + + return EXIT_SUCCESS; } -void product_exit( ProductOrder *product ) +void product_exit (ProductOrder *product) { - free( product->response->string ); - free( product->response ); - free( product->orderID ); - free( product->orderRequest ); - free( product->payUrl ); - free( product->checkUrl ); - free( product->currency ); - product->amount = NULL; - product->product = NULL; - product->paid = false; + free (product->response->string); + free (product->response); + free (product->orderID); + free (product->orderRequest); + free (product->payUrl); + free (product->checkUrl); + free (product->currency); + product->amount = NULL; + product->product = NULL; + product->paid = false; } -int product_set_check_url( ProductOrder *product ) +int product_set_check_url (ProductOrder *product) { - size_t len = strlen(BACKEND_BASE_URL) + strlen( CHECK ); - char *url = realloc( product->checkUrl, len + strlen(ORDER_CHECK) + strlen(product->orderID) + 1 ); - if( !url ){ - printf( "could not allocate memory for order url\n" ); - return EXIT_FAILURE; - } + size_t len = strlen (BACKEND_BASE_URL) + strlen (CHECK); + char *url = realloc (product->checkUrl, len + strlen (ORDER_CHECK) + strlen ( + product->orderID) + 1); + if ( ! url ) + { + printf ("could not allocate memory for order url\n"); + return EXIT_FAILURE; + } - product->checkUrl = url; - sprintf( product->checkUrl, "%s%s%s%s", BACKEND_BASE_URL, CHECK, ORDER_CHECK, product->orderID ); - printf( "Url to check payment: %s\n\n", product->checkUrl ); + product->checkUrl = url; + sprintf (product->checkUrl, "%s%s%s%s", BACKEND_BASE_URL, CHECK, ORDER_CHECK, + product->orderID); + printf ("Url to check payment: %s\n\n", product->checkUrl); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } -int product_set_order_id( ProductOrder *product ) +int product_set_order_id (ProductOrder *product) { - return taler_parse_json( product->response, JSON_ORDER_ID, &(product->orderID), false ); + return taler_parse_json (product->response, JSON_ORDER_ID, + &(product->orderID), false); } -int product_set_pay_url( ProductOrder *product ) +int product_set_pay_url (ProductOrder *product) { - return taler_parse_json( product->response, JSON_PAY_URI, &(product->payUrl), false ); + return taler_parse_json (product->response, JSON_PAY_URI, &(product->payUrl), + false); } -int product_set_paid_status( ProductOrder *product ) +int product_set_paid_status (ProductOrder *product) { - char *temp = NULL; - if( taler_alloc_string( &temp, 1) == EXIT_SUCCESS ){ - taler_parse_json( product->response, JSON_PAID, &temp, true ); - if( strcmp( temp, "true" ) == 0 ) - product->paid = true; - free(temp); - return EXIT_SUCCESS; - } + char *temp = NULL; + if ( taler_alloc_string (&temp, 1) == EXIT_SUCCESS ) + { + taler_parse_json (product->response, JSON_PAID, &temp, true); + if ( strcmp (temp, "true") == 0 ) + product->paid = true; + free (temp); + return EXIT_SUCCESS; + } - return EXIT_FAILURE; + return EXIT_FAILURE; } diff --git a/src/product.h b/src/product.h @@ -32,36 +32,38 @@ the attributes. #include <stdlib.h> #include <stdbool.h> -typedef struct TalerResponse{ - char *string; - size_t size; +typedef struct TalerResponse +{ + char *string; + size_t size; } TalerResponse; -typedef struct ProductOrder{ - char *product; - char *currency; - char *amount; - char *orderRequest; - char *orderID; - char *checkUrl; - char *payUrl; - bool paid; - TalerResponse *response; +typedef struct ProductOrder +{ + char *product; + char *currency; + char *amount; + char *orderRequest; + char *orderID; + char *checkUrl; + char *payUrl; + bool paid; + TalerResponse *response; } ProductOrder; -int product_init( ProductOrder *product, const char* currency ); +int product_init (ProductOrder *product, const char*currency); -void product_exit( ProductOrder *product ); +void product_exit (ProductOrder *product); -void product_reset( ProductOrder *product ); +void product_reset (ProductOrder *product); -int product_set_check_url( ProductOrder *product ); +int product_set_check_url (ProductOrder *product); -int product_set_order_id( ProductOrder *product ); +int product_set_order_id (ProductOrder *product); -int product_set_pay_url( ProductOrder *product ); +int product_set_pay_url (ProductOrder *product); -int product_set_paid_status( ProductOrder *product ); +int product_set_paid_status (ProductOrder *product); #endif // PRODUCT_H diff --git a/src/wallet.c b/src/wallet.c @@ -30,95 +30,106 @@ along with #include "wallet.h" -int wallet_select_aid( nfc_device *pnd ) +int wallet_select_aid (nfc_device *pnd) { - uint8_t response[] = { 0x00, 0x00 }; - - int size = sizeof( select_file ) + sizeof( taler_aid ); - uint8_t message[size]; - if( concat_message( select_file, sizeof(select_file), taler_aid, message, size ) ) - return EXIT_FAILURE; - - printf( "wallet_select_aid: Selecting Taler apk using AID: " ); - for( unsigned int i = 0; i < sizeof(taler_aid); ++i ) - printf( "%.2x", taler_aid[i] ); - printf( "\n" ); - - if( nfc_initiator_transceive_bytes( pnd, message, size, response, sizeof(response), TRANSMIT_TIMEOUT ) < 0 ) { - printf( "wallet_select_aid: Failed to select apk\n\n" ); - return EXIT_FAILURE; - } - - return check_response( response, sizeof(response) ); + uint8_t response[] = { 0x00, 0x00 }; + + int size = sizeof(select_file) + sizeof(taler_aid); + uint8_t message[size]; + if ( concat_message (select_file, sizeof(select_file), taler_aid, message, + size) ) + return EXIT_FAILURE; + + printf ("wallet_select_aid: Selecting Taler apk using AID: "); + for ( unsigned int i = 0; i < sizeof(taler_aid); ++i ) + printf ("%.2x", taler_aid[i]); + printf ("\n"); + + if ( nfc_initiator_transceive_bytes (pnd, message, size, response, + sizeof(response), TRANSMIT_TIMEOUT) < 0 ) + { + printf ("wallet_select_aid: Failed to select apk\n\n"); + return EXIT_FAILURE; + } + + return check_response (response, sizeof(response) ); } -int wallet_put_message( nfc_device *pnd, const char *msg, size_t msgSize ) +int wallet_put_message (nfc_device *pnd, const char *msg, size_t msgSize) { - uint8_t response[] = { 0x00, 0x00 }; + uint8_t response[] = { 0x00, 0x00 }; - int size = sizeof( put_data ) + msgSize; - uint8_t message[size]; - if( concat_message( put_data, sizeof(put_data), (const uint8_t*)msg, message, size ) ) - return EXIT_FAILURE; + int size = sizeof(put_data) + msgSize; + uint8_t message[size]; + if ( concat_message (put_data, sizeof(put_data), (const uint8_t*) msg, + message, size) ) + return EXIT_FAILURE; - printf( "wallet_put_messsage: Sending 'PUT DATA' command to wallet\n" ); + printf ("wallet_put_messsage: Sending 'PUT DATA' command to wallet\n"); - if( nfc_initiator_transceive_bytes( pnd, message, size, response, sizeof(response), TRANSMIT_TIMEOUT ) < 0 ) { - printf( "wallet_put_message: Failed to put message\n\n" ); - return EXIT_FAILURE; - } + if ( nfc_initiator_transceive_bytes (pnd, message, size, response, + sizeof(response), TRANSMIT_TIMEOUT) < 0 ) + { + printf ("wallet_put_message: Failed to put message\n\n"); + return EXIT_FAILURE; + } - return check_response( response, sizeof(response) ); + return check_response (response, sizeof(response) ); } -int wallet_transmit( nfc_device *pnd, const char *msg, size_t msgLen ) +int wallet_transmit (nfc_device *pnd, const char *msg, size_t msgLen) { - if( !msg ) { - printf( "wallet_transmit: No message to send\n\n" ); - return EXIT_FAILURE; - } - - if( wallet_select_aid( pnd ) ) - return EXIT_FAILURE; - printf( "wallet_transmit: Taler wallet apk selected\n\n" ); - if( wallet_put_message( pnd, msg, msgLen ) ) - return EXIT_FAILURE; - printf( "wallet_transmit: Transmitted message to taler wallet\n\n"); - - return EXIT_SUCCESS; + if ( ! msg ) + { + printf ("wallet_transmit: No message to send\n\n"); + return EXIT_FAILURE; + } + + if ( wallet_select_aid (pnd) ) + return EXIT_FAILURE; + printf ("wallet_transmit: Taler wallet apk selected\n\n"); + if ( wallet_put_message (pnd, msg, msgLen) ) + return EXIT_FAILURE; + printf ("wallet_transmit: Transmitted message to taler wallet\n\n"); + + return EXIT_SUCCESS; } -int check_response( uint8_t *response, uint8_t responseLen ) +int check_response (uint8_t *response, uint8_t responseLen) { - if( strcmp((char*)response, APDU_SUCCESS ) == 0 ){ - printf("Transmission success\n"); - } else { - printf("Transmission failure, return code: "); - for( uint8_t i = 0; i < responseLen; ++i ){ - printf( "%.2x ", response[i] ); - } - printf("\n"); - return EXIT_FAILURE; + if ( strcmp ((char*) response, APDU_SUCCESS) == 0 ) + { + printf ("Transmission success\n"); + } + else { + printf ("Transmission failure, return code: "); + for ( uint8_t i = 0; i < responseLen; ++i ) { + printf ("%.2x ", response[i]); } + printf ("\n"); + return EXIT_FAILURE; + } - return EXIT_SUCCESS; + return EXIT_SUCCESS; } -int concat_message(const uint8_t* command, size_t commandSize, const uint8_t* message, uint8_t *retMsg, size_t returnSize ) +int concat_message (const uint8_t*command, size_t commandSize, const + uint8_t*message, uint8_t *retMsg, size_t returnSize) { - if( !command || !message ){ - printf( "concat_message: command and message can't be null" ); - return EXIT_FAILURE; - } - - uint8_t i = 0; - for( ; i < commandSize; ++i ){ - retMsg[i] = command[i]; - } - for( ; i < returnSize; ++i){ - retMsg[i] = message[i - commandSize]; - } - - return EXIT_SUCCESS; + if ( ! command || ! message ) + { + printf ("concat_message: command and message can't be null"); + return EXIT_FAILURE; + } + + uint8_t i = 0; + for (; i < commandSize; ++i ) { + retMsg[i] = command[i]; + } + for (; i < returnSize; ++i) { + retMsg[i] = message[i - commandSize]; + } + + return EXIT_SUCCESS; } diff --git a/src/wallet.h b/src/wallet.h @@ -44,14 +44,15 @@ static const uint8_t put_data[] = { 0x00, 0xDA, 0x01, 0x00, 0x7c, 0x01 }; -int wallet_select_aid( nfc_device *pnd ); +int wallet_select_aid (nfc_device *pnd); -int wallet_put_message( nfc_device *pnd, const char *msg, size_t msgSize ); +int wallet_put_message (nfc_device *pnd, const char *msg, size_t msgSize); -int wallet_transmit( nfc_device* pnd, const char *msg, size_t msgLen ); +int wallet_transmit (nfc_device*pnd, const char *msg, size_t msgLen); -int concat_message( const uint8_t* command, size_t commandSize, const uint8_t *message, uint8_t *retMsg, size_t returnSize ); +int concat_message (const uint8_t*command, size_t commandSize, const + uint8_t *message, uint8_t *retMsg, size_t returnSize); -int check_response( uint8_t *response, uint8_t responseLen ); +int check_response (uint8_t *response, uint8_t responseLen); #endif // WALLET_H