taler-mdb

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

commit 681e05cd9d3f4ca9566ad9247cd9538c01273a97
parent 376c44387c968924403618bd2e594a3332ceafe7
Author: Christian Grothoff <christian@grothoff.org>
Date:   Thu, 31 Oct 2019 12:55:25 +0100

reindent

Diffstat:
Msrc/communication.c | 67++++++++++++++++++++++++++++++++++++++++++-------------------------
Msrc/configuration.h | 1-
Msrc/main.c | 40+++++++++++++++++++++-------------------
Msrc/product.c | 50++++++++++++++++++++++++++++++++------------------
Msrc/product.h | 1-
Msrc/wallet.c | 60++++++++++++++++++++++++++++++++++++------------------------
Msrc/wallet.h | 2--
7 files changed, 131 insertions(+), 90 deletions(-)

diff --git a/src/communication.c b/src/communication.c @@ -30,9 +30,11 @@ along with #include "wallet.h" #include "configuration.h" -int taler_init (CURL **curl) + +int +taler_init (CURL **curl) { - if ( *curl != NULL ) + if (*curl != NULL) { printf ("taler_init: curl handle already initialized\n"); return EXIT_FAILURE; @@ -41,7 +43,7 @@ int taler_init (CURL **curl) // initialize curl CURLcode result = curl_global_init (CURL_GLOBAL_ALL); *curl = curl_easy_init (); - if ( ! (*curl) ||(result != CURLE_OK) ) + if (! (*curl) || (result != CURLE_OK) ) { printf ("taler_init: could not inizialize curl handle\n"); return EXIT_FAILURE; @@ -50,16 +52,20 @@ int taler_init (CURL **curl) return EXIT_SUCCESS; } -void taler_exit (CURL **curl) + +void +taler_exit (CURL **curl) { 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) ) + if (! (*string) ) { printf ("taler_alloc: unable to allocate string"); exit (EXIT_FAILURE); @@ -67,14 +73,16 @@ int taler_alloc_string (char **string, size_t size) 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 ) + if (! tempString) { printf ("Allocation failure"); return EXIT_FAILURE; @@ -88,7 +96,9 @@ static size_t _taler_write_response (void *contents, size_t size, size_t nmemb, return realSize; } -int taler_create_order_req (ProductOrder *product) + +int +taler_create_order_req (ProductOrder *product) { char buffer[256]; @@ -96,7 +106,7 @@ int taler_create_order_req (ProductOrder *product) product->amount, product->product); char*temp = realloc (product->orderRequest, strlen (buffer) + 1); - if ( ! temp ) + if (! temp) { printf ("could not allocate order\n"); return EXIT_FAILURE; @@ -109,7 +119,9 @@ int taler_create_order_req (ProductOrder *product) 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; @@ -134,7 +146,7 @@ int taler_create_order (CURL *curl, ProductOrder *product) // perform the call CURLcode result = curl_easy_perform (curl); - if ( result != CURLE_OK ) + if (result != CURLE_OK) { printf ("could not communicate with \"%s\"\n", url); return EXIT_FAILURE; @@ -149,7 +161,9 @@ int taler_create_order (CURL *curl, ProductOrder *product) 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; @@ -171,7 +185,7 @@ int taler_check_payment_status (CURL *curl, ProductOrder *product) // perform the call CURLcode result = curl_easy_perform (curl); - if ( result != CURLE_OK ) + if (result != CURLE_OK) { printf ("could not communicate with \"%s\"\n", product->checkUrl); return EXIT_FAILURE; @@ -186,43 +200,46 @@ int taler_check_payment_status (CURL *curl, ProductOrder *product) 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) ) + if (! (*returnStr) ) taler_alloc_string (returnStr, 1); char *result = NULL; char *temp = NULL; char mbr[64]; - if ( isBoolean ) + if (isBoolean) { // If the wanted member is of type boolean sprintf (mbr, "\"%s\": true", memberName); - if ( (temp = strstr (response->string, mbr)) != NULL ) + if ( (temp = strstr (response->string, mbr)) != NULL) result = "true"; else result = "false"; } - else{ + else + { // String type members sprintf (mbr, "\"%s\":", memberName); - if ( (temp = strstr (response->string, mbr)) != NULL ) + if ( (temp = strstr (response->string, mbr)) != NULL) { - if ( (temp = strstr (response->string, ": ")) != NULL ) + if ( (temp = strstr (response->string, ": ")) != NULL) { result = strtok (temp, "\""); result = strtok (NULL, "\""); } } } - if ( ! result ) + 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 ) + if (! temp) { printf ("taler_parse_json: could not allocate memory for member\n"); return EXIT_FAILURE; diff --git a/src/configuration.h b/src/configuration.h @@ -39,7 +39,6 @@ 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" diff --git a/src/main.c b/src/main.c @@ -43,24 +43,25 @@ 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 ) + 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 ) + while (1) { - if ( pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL) != 0 ) + 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) + if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0) { printf ("Error setting thread cancelling state\n"); } @@ -70,11 +71,12 @@ void *start_nfc_transmission (void *ignored) } -int main ( ) +int +main ( ) { // initialize nfc nfc_init (&context); - if ( ! context ) + if (! context) { printf ("Unable to init libnfc\n"); return EXIT_FAILURE; @@ -82,25 +84,25 @@ int main ( ) // inizialize taler CURL *curl = NULL; - if ( taler_init (&curl) ) + if (taler_init (&curl) ) { printf ("Unable to init taler communication\n"); return EXIT_FAILURE; } // inizialize product - if ( product_init (&product, CURRENCY) ) + if (product_init (&product, CURRENCY) ) { printf ("Unable to init product\n"); return EXIT_FAILURE; } - while ( true ) + while (true) { printf ("Waiting for MBD input\n"); printf ("Enter to simulate Snickers, x to quit\n"); - if ( getchar () == 'x' ) + if (getchar () == 'x') break; // DEMO snickers @@ -112,7 +114,7 @@ int main ( ) taler_create_order_req (&product); // create the order - while ( taler_create_order (curl, &product) ) + while (taler_create_order (curl, &product) ) ; // store the order id into the struct @@ -122,7 +124,7 @@ int main ( ) product_set_check_url (&product); // check the payment status for the first time - while ( taler_check_payment_status (curl, &product) ) + 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 @@ -130,19 +132,19 @@ int main ( ) // start a thread to send payment request to taler wallet pthread_t nfcThread; - if ( pthread_create (&nfcThread, NULL, start_nfc_transmission, NULL) ) + 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 ) + while (! product.paid) { printf ("Order payment processing\n"); fflush (stdout); sleep (5); - while ( taler_check_payment_status (curl, &product) ) + while (taler_check_payment_status (curl, &product) ) ; // set the boolean paid member of ProductOrder struct product_set_paid_status (&product); @@ -152,17 +154,17 @@ int main ( ) printf ("Order no.: %s paid!\n\n", product.orderID); // send cancel request to nfc thread - while ( pthread_cancel (nfcThread) != 0 ) + while (pthread_cancel (nfcThread) != 0) { printf ("Error sending cancel request to thread for nfc transmission"); } void*res; - if ( pthread_join (nfcThread, &res) == 0 ) + if (pthread_join (nfcThread, &res) == 0) { printf ("Thread for nfc transmission finished\n"); fflush (stdout); } - else if ( res == PTHREAD_CANCELED ) + else if (res == PTHREAD_CANCELED) { printf ("Thread for nfc transmission finished\n"); fflush (stdout); diff --git a/src/product.c b/src/product.c @@ -31,16 +31,20 @@ the attributes. #include "configuration.h" #include "communication.h" -void product_reset (ProductOrder *product) + +void +product_reset (ProductOrder *product) { 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 ) + if (! product) { printf ("product_init: product order struct must be provided\n"); return EXIT_FAILURE; @@ -48,22 +52,22 @@ int product_init (ProductOrder *product, const char *currency) // 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 ) + 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) ) + if (taler_alloc_string (&(product->response->string), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->orderID), 1) ) + if (taler_alloc_string (&(product->orderID), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->orderRequest), 1) ) + if (taler_alloc_string (&(product->orderRequest), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->payUrl), 1) ) + if (taler_alloc_string (&(product->payUrl), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->checkUrl), 1) ) + if (taler_alloc_string (&(product->checkUrl), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->currency), strlen (currency) + 1) ) + if (taler_alloc_string (&(product->currency), strlen (currency) + 1) ) return EXIT_FAILURE; strcpy (product->currency, currency); product->amount = NULL; @@ -73,7 +77,9 @@ int product_init (ProductOrder *product, const char *currency) return EXIT_SUCCESS; } -void product_exit (ProductOrder *product) + +void +product_exit (ProductOrder *product) { free (product->response->string); free (product->response); @@ -87,12 +93,14 @@ void product_exit (ProductOrder *product) 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 ) + if (! url) { printf ("could not allocate memory for order url\n"); return EXIT_FAILURE; @@ -106,25 +114,31 @@ int product_set_check_url (ProductOrder *product) 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); } -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); } -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 ) + if (taler_alloc_string (&temp, 1) == EXIT_SUCCESS) { taler_parse_json (product->response, JSON_PAID, &temp, true); - if ( strcmp (temp, "true") == 0 ) + if (strcmp (temp, "true") == 0) product->paid = true; free (temp); return EXIT_SUCCESS; diff --git a/src/product.h b/src/product.h @@ -51,7 +51,6 @@ typedef struct ProductOrder TalerResponse *response; } ProductOrder; - int product_init (ProductOrder *product, const char*currency); void product_exit (ProductOrder *product); diff --git a/src/wallet.c b/src/wallet.c @@ -30,23 +30,24 @@ 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) ) + 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 ) + 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 ) + 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; @@ -55,20 +56,22 @@ int wallet_select_aid (nfc_device *pnd) 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 }; int size = sizeof(put_data) + msgSize; uint8_t message[size]; - if ( concat_message (put_data, sizeof(put_data), (const uint8_t*) msg, - 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"); - if ( nfc_initiator_transceive_bytes (pnd, message, size, response, - sizeof(response), TRANSMIT_TIMEOUT) < 0 ) + 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; @@ -78,33 +81,38 @@ 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) { - if ( ! msg ) + if (! msg) { printf ("wallet_transmit: No message to send\n\n"); return EXIT_FAILURE; } - if ( wallet_select_aid (pnd) ) + if (wallet_select_aid (pnd) ) return EXIT_FAILURE; printf ("wallet_transmit: Taler wallet apk selected\n\n"); - if ( wallet_put_message (pnd, msg, msgLen) ) + 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 ) + if (strcmp ((char*) response, APDU_SUCCESS) == 0) { printf ("Transmission success\n"); } - else { + else + { printf ("Transmission failure, return code: "); - for ( uint8_t i = 0; i < responseLen; ++i ) { + for (uint8_t i = 0; i < responseLen; ++i) + { printf ("%.2x ", response[i]); } printf ("\n"); @@ -114,20 +122,24 @@ int check_response (uint8_t *response, uint8_t responseLen) 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 ) + if (! command || ! message) { printf ("concat_message: command and message can't be null"); return EXIT_FAILURE; } uint8_t i = 0; - for (; i < commandSize; ++i ) { + for (; i < commandSize; ++i) + { retMsg[i] = command[i]; } - for (; i < returnSize; ++i) { + for (; i < returnSize; ++i) + { retMsg[i] = message[i - commandSize]; } diff --git a/src/wallet.h b/src/wallet.h @@ -42,8 +42,6 @@ static const uint8_t taler_aid[] = { 0xA0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 }; static const uint8_t select_file[] = { 0x00, 0xA4, 0x04, 0x00, 0x07 }; static const uint8_t put_data[] = { 0x00, 0xDA, 0x01, 0x00, 0x7c, 0x01 }; - - int wallet_select_aid (nfc_device *pnd); int wallet_put_message (nfc_device *pnd, const char *msg, size_t msgSize);