taler-mdb

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

commit f85b490473d4e1710ccfbc8e3148ce5bb31db956
parent b6227efb61c02024adc70543bc76a92afbe8db6a
Author: BOSS_Marco <bossm8@students.bfh.ch>
Date:   Mon,  4 Nov 2019 21:08:23 +0100

GNUnet logs added, -h option added

Diffstat:
Msrc/communication.c | 55++++++++++++++++++++++++++-----------------------------
Msrc/communication.h | 14+++++++-------
Msrc/configuration.h | 12+++++++-----
Msrc/main.c | 75++++++++++++++++++++++++++++++++++++++++++---------------------------------
Msrc/nfc.c | 44+++++++++++++++++---------------------------
Msrc/nfc.h | 5++---
Msrc/product.c | 46++++++++++++++++++++++++----------------------
Msrc/product.h | 14+++++++-------
Msrc/wallet.c | 99++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/wallet.h | 14+++++++++-----
10 files changed, 212 insertions(+), 166 deletions(-)

diff --git a/src/communication.c b/src/communication.c @@ -26,50 +26,47 @@ along with #include <string.h> #include <stdbool.h> #include <jansson.h> +#include <gnunet/platform.h> +#include <gnunet/gnunet_util_lib.h> #include "communication.h" #include "wallet.h" #include "configuration.h" -int taler_init (CURL **curl) +int SNACK_taler_init (CURL **curl) { CURLcode result; - if ( *curl != NULL ) - { - printf ("taler_init: curl handle already initialized\n"); - return EXIT_FAILURE; - } - /* initialize curl */ 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"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_init: could not inizialize curl handle\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } -void taler_exit (CURL **curl) + +void SNACK_taler_exit (CURL **curl) { curl_easy_cleanup (*curl); curl_global_cleanup (); } -int taler_alloc_string (char **string, size_t size) +int SNACK_taler_alloc_string (char **string, size_t size) { *string = malloc (size); if ( ! (*string) ) { - printf ("taler_alloc: unable to allocate string"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_alloc: unable to allocate string\n"); exit (EXIT_FAILURE); } return EXIT_SUCCESS; } -static size_t _taler_write_response (void *contents, size_t size, size_t nmemb, +static size_t _SNACK_taler_write_response (void *contents, size_t size, size_t nmemb, void *userp) { char *tempString; @@ -79,7 +76,7 @@ static size_t _taler_write_response (void *contents, size_t size, size_t nmemb, tempString = realloc (response->string, response->size + realSize + 1); if ( ! tempString ) { - printf ("Allocation failure"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_write_response: allocation failure\n"); return EXIT_FAILURE; } @@ -91,7 +88,7 @@ static size_t _taler_write_response (void *contents, size_t size, size_t nmemb, return realSize; } -int taler_create_order_req (ProductOrder *product) +int SNACK_taler_create_order_req (ProductOrder *product) { json_t *orderReq; char*temp; @@ -113,7 +110,7 @@ int taler_create_order_req (ProductOrder *product) amountStr, JSON_REQ_FULFILLMENT, fulfillmentUrl); if ( ! orderReq ) { - printf ("taler_create_order_req: error creating json object\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_create_order_req: error creating json object\n"); return EXIT_FAILURE; } @@ -121,7 +118,7 @@ int taler_create_order_req (ProductOrder *product) char *buffer = json_dumps (orderReq, JSON_COMPACT); if ( ! buffer ) { - printf ("taler_create_order_req: error converting json to string\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_create_order_req: error converting json to string\n"); return EXIT_FAILURE; } @@ -129,7 +126,7 @@ int taler_create_order_req (ProductOrder *product) temp = realloc (product->orderRequest, strlen (buffer) + 1); if ( ! temp ) { - printf ("could not allocate order\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_create_order_req: could not allocate order\n"); return EXIT_FAILURE; } product->orderRequest = temp; @@ -147,7 +144,7 @@ int taler_create_order_req (ProductOrder *product) return EXIT_SUCCESS; } -int taler_create_order (CURL *curl, ProductOrder *product) +int SNACK_taler_create_order (CURL *curl, ProductOrder *product) { char url[ strlen (BACKEND_BASE_URL) + strlen (ORDER) + 1 ]; struct curl_slist *list = NULL; @@ -169,14 +166,14 @@ int taler_create_order (CURL *curl, ProductOrder *product) 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_WRITEFUNCTION, _SNACK_taler_write_response); curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response); /* perform the call */ result = curl_easy_perform (curl); if ( result != CURLE_OK ) { - printf ("could not communicate with \"%s\"\n", url); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_create_order: could not communicate with \"%s\"\n", url); return EXIT_FAILURE; } @@ -192,7 +189,7 @@ int taler_create_order (CURL *curl, ProductOrder *product) return EXIT_SUCCESS; } -int taler_check_payment_status (CURL *curl, ProductOrder *product) +int SNACK_taler_check_payment_status (CURL *curl, ProductOrder *product) { struct curl_slist *list = NULL; CURLcode result; @@ -211,14 +208,14 @@ int taler_check_payment_status (CURL *curl, ProductOrder *product) 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_WRITEFUNCTION, _SNACK_taler_write_response); curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response); /* perform the call */ result = curl_easy_perform (curl); if ( result != CURLE_OK ) { - printf ("could not communicate with \"%s\"\n", product->checkUrl); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_check_payment_status: could not communicate with \"%s\"\n", product->checkUrl); return EXIT_FAILURE; } @@ -231,7 +228,7 @@ int taler_check_payment_status (CURL *curl, ProductOrder *product) return EXIT_SUCCESS; } -int taler_parse_json (const TalerResponse *response, const char *memberName, +int SNACK_taler_parse_json (const TalerResponse *response, const char *memberName, char **returnStr) { /* json variables */ @@ -243,25 +240,25 @@ int taler_parse_json (const TalerResponse *response, const char *memberName, /* if the provided string was not allocated before allocate initial size 1 to * realloc later */ if ( ! (*returnStr) ) - taler_alloc_string (returnStr, 1); + SNACK_taler_alloc_string (returnStr, 1); /* load the json with th string provided from taler in the response struct */ root = json_loads (response->string, JSON_DISABLE_EOF_CHECK, &error); if ( ! root ) { - printf ("taler_parse_json: %s\n", error.text); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: %s\n", error.text); return EXIT_FAILURE; } char *result = NULL; if ( json_unpack (root, "{s:s}", memberName, &result) < 0 ) { - printf ("taler_parse_json: no member named \"%s\" found!\n", memberName); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: no member named \"%s\" found!\n", memberName); json_decref (root); return EXIT_FAILURE; } if ( ! result ) { - printf ("taler_parse_json: no member named \"%s\" found!\n", memberName); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: no member named \"%s\" found!\n", memberName); json_decref (root); return EXIT_FAILURE; } @@ -269,7 +266,7 @@ int taler_parse_json (const TalerResponse *response, const char *memberName, temp = realloc (*returnStr, strlen (result) + 1); if ( ! temp ) { - printf ("taler_parse_json: could not allocate memory for member\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: could not allocate memory for member\n"); json_decref (root); return EXIT_FAILURE; } diff --git a/src/communication.h b/src/communication.h @@ -34,19 +34,19 @@ along with #include "product.h" -int taler_init (CURL **curl); +int SNACK_taler_init (CURL **curl); -void taler_exit (CURL **curl); +void SNACK_taler_exit (CURL **curl); -int taler_alloc_string (char **string, size_t size); +int SNACK_taler_alloc_string (char **string, size_t size); -int taler_create_order_req (ProductOrder *product); +int SNACK_taler_create_order_req (ProductOrder *product); -int taler_create_order (CURL *curl, ProductOrder *product); +int SNACK_taler_create_order (CURL *curl, ProductOrder *product); -int taler_check_payment_status (CURL *curl, ProductOrder *product); +int SNACK_taler_check_payment_status (CURL *curl, ProductOrder *product); -int taler_parse_json (const TalerResponse *response, const char *memberName, +int SNACK_taler_parse_json (const TalerResponse *response, const char *memberName, char **returnStr); #endif // COMM_H diff --git a/src/configuration.h b/src/configuration.h @@ -29,6 +29,8 @@ along with #include <stdio.h> #include <inttypes.h> +static const char*const CURRENCY = "KUDOS"; + static const char*const ORDER = "/order"; static const char*const CHECK = "/check-payment"; @@ -50,15 +52,15 @@ static const char*const JSON_REQ_SUMMARY = "summary"; static const char*const JSON_REQ_AMOUNT = "amount"; static const char*const JSON_REQ_FULFILLMENT = "fulfillment_url"; -/* New AID */ -// static uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 }; - -/* Demo AID until uptade */ -static const uint8_t taler_aid[] = { 0xA0, 0x00, 0x00, 0x02, 0x47, 0x10, 0x01 }; +/* Wallet AID */ +static const uint8_t taler_aid[] = { 0xF0, 0x00, 0x54, 0x41, 0x4c, 0x45, 0x52 }; /* APDU commands */ #define APDU_SUCCESS "\x90\x00" static const uint8_t select_file[] = { 0x00, 0xA4, 0x04, 0x00, 0x07 }; static const uint8_t put_data[] = { 0x00, 0xDA, 0x01, 0x00, 0x7c, 0x01 }; +/* tunneling */ +static const uint8_t get_data[] = { 0x00, 0xCA, 0x01, 0x00, 0x00, 0x00 }; + #endif // URL_H diff --git a/src/main.c b/src/main.c @@ -31,19 +31,19 @@ along with #include <unistd.h> #include <nfc/nfc.h> #include <curl/curl.h> +#include <gnunet/platform.h> +#include <gnunet/gnunet_util_lib.h> #include "nfc.h" #include "communication.h" +#include "configuration.h" #include "product.h" -const char *CURRENCY = "KUDOS"; - - ProductOrder product; -nfc_context *context = NULL; +nfc_context *context; -void *start_nfc_transmission (void *ignored) +void *_SNACK_start_nfc_transmission (void *ignored) { /* supress warning about unused variable */ (void) ignored; @@ -60,7 +60,7 @@ void *start_nfc_transmission (void *ignored) { printf ("Error setting thread cancelling state\n"); } - nfc_transmit (context, product.payUrl, strlen (product.payUrl) ); + SNACK_nfc_transmit (context, product.payUrl, strlen (product.payUrl) ); if ( pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0) { printf ("Error setting thread cancelling state\n"); @@ -70,29 +70,40 @@ void *start_nfc_transmission (void *ignored) return EXIT_SUCCESS; } - -int main ( ) +int main( int argc, char* const* argv ) { + CURL *curl; + + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_option_help( "This is an application for snack machines with nfc. Running with GNU taler.\n" ), + GNUNET_GETOPT_OPTION_END + }; + + if( GNUNET_GETOPT_run("taler-mdb\n", options, argc, argv) <= 0 ) + return EXIT_SUCCESS; + + GNUNET_assert + (GNUNET_OK == GNUNET_log_setup("taler-mdb", "DEBUG", NULL)); //"taler-mdb.log")); + /* initialize nfc */ nfc_init (&context); if ( ! context ) { - printf ("Unable to init libnfc\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unable to init nfc\n"); return EXIT_FAILURE; } /* inizialize taler */ - CURL *curl = NULL; - if ( taler_init (&curl) ) + if ( SNACK_taler_init (&curl) ) { - printf ("Unable to init taler communication\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unable to init taler communication\n"); return EXIT_FAILURE; } /* inizialize product */ - if ( product_init (&product, CURRENCY) ) + if ( SNACK_product_init (&product, CURRENCY) ) { - printf ("Unable to init product\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unable to init product structure\n"); return EXIT_FAILURE; } @@ -110,47 +121,46 @@ int main ( ) /* create the order request */ - taler_create_order_req (&product); + SNACK_taler_create_order_req (&product); /* create the order */ - while ( taler_create_order (curl, &product) ) + while ( SNACK_taler_create_order (curl, &product) ) ; /* store the order id into the struct */ - product_set_order_id (&product); + SNACK_product_set_order_id (&product); /* store the url to check wheter the payment happened or not */ - product_set_check_url (&product); + SNACK_product_set_check_url (&product); /* check the payment status for the first time */ - while ( taler_check_payment_status (curl, &product) ) + while ( SNACK_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); + SNACK_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) ) + if ( pthread_create (&nfcThread, NULL, _SNACK_start_nfc_transmission, NULL) ) { - printf ("Could not create thread"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Thread creation failed\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); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Order payment processing\n"); sleep (5); - while ( taler_check_payment_status (curl, &product) ) + while ( SNACK_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") ); + SNACK_product_set_paid_status (&product); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Payment status paid: %s\n", (product.paid ? "true":"false") ); } - printf ("Order no.: %s paid!\n\n", product.orderID); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Order no.: %s paid!\n", product.orderID); + /* ----------------- Here comes the code for releasing the product @@ -176,15 +186,14 @@ int main ( ) } /* reset the product */ - product_reset (&product); + SNACK_product_reset (&product); } /* clear all initialized data */ nfc_exit (context); - product_exit (&product); - taler_exit (&curl); + SNACK_product_exit (&product); + SNACK_taler_exit (&curl); return EXIT_SUCCESS; - } diff --git a/src/nfc.c b/src/nfc.c @@ -25,6 +25,8 @@ along with #include <string.h> #include <unistd.h> +#include <gnunet/platform.h> +#include <gnunet/gnunet_util_lib.h> #include "nfc.h" #include "wallet.h" @@ -35,42 +37,38 @@ along with #define UID_LEN_LOWER 4 -int nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t urlSize) +int SNACK_nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t urlSize) { - nfc_device *pnd = NULL; + nfc_device *pnd; nfc_target nt; - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// MEMORY LEAK? NFC OPEN? - - pnd = nfc_open (context, NULL); // NULL could be replaced with connstring if the correct is known + pnd = nfc_open (context, NULL); if ( ! pnd ) { - printf ("Unable to open NFC device\n"); - nfc_close (pnd); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_nfc_transmit: Unable to open nfc device\n"); return EXIT_FAILURE; } /* initialize device as reader */ if ( nfc_initiator_init (pnd) < 0 ) { - nfc_perror (pnd, "nfc_initiator_init"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Snack_nfc_transmit: nfc_initator_init error: %s", nfc_strerror(pnd) ); nfc_close (pnd); return EXIT_FAILURE; } - printf ("Device %s opened: '%s'\n\n", nfc_device_get_name (pnd), - nfc_device_get_connstring (pnd) ); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "SNACK_nfc_transmit: device: %s\n", nfc_device_get_name (pnd)); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "SNACK_nfc_transmit: connstring: %s\n", nfc_device_get_connstring (pnd)); /* connect to a target device */ - if ( nfc_connect_target (pnd, &nt) ) + if ( SNACK_nfc_connect_target (pnd, &nt) ) { nfc_close (pnd); return EXIT_FAILURE; } - ; /* send the message to the wallet */ - if ( wallet_transmit (pnd, talerPayUrl, urlSize) ) + if ( SNACK_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); @@ -84,7 +82,7 @@ int nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t urlSize) return EXIT_SUCCESS; } -int nfc_connect_target (nfc_device *pnd, nfc_target *nt) +int SNACK_nfc_connect_target (nfc_device *pnd, nfc_target *nt) { /* ctr for how many tries to connect a target */ int ctr = 2; @@ -94,7 +92,7 @@ int nfc_connect_target (nfc_device *pnd, nfc_target *nt) .nbr = NBR_106, } }; - printf ("nfc_connect_target: trying to connect to target\n"); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "SNACK_nfc_connect_target: trying to connect to target\n"); while ( ctr > 0 ) { @@ -103,16 +101,16 @@ int nfc_connect_target (nfc_device *pnd, nfc_target *nt) if ( nfc_initiator_select_passive_target (pnd, nmMifare[0], NULL, 0, nt) <= 0 ) { - printf ("nfc_connect_target: failed to connect\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_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"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_nfc_connect_target: failed to connect\n"); } else { - printf ("nfc_connect_target: Target selected!\n"); - nfc_display_target_uid (nt); + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "SNACK_nfc_connect_target: Target selected!\n"); + SNACK_print_hex_info( "UID", nt->nti.nai.abtUid, nt->nti.nai.szUidLen ); return EXIT_SUCCESS; } sleep (1); @@ -122,11 +120,3 @@ int nfc_connect_target (nfc_device *pnd, nfc_target *nt) return EXIT_FAILURE; } -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"); -} diff --git a/src/nfc.h b/src/nfc.h @@ -27,9 +27,8 @@ along with #include <stdio.h> #include <nfc/nfc.h> -int nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t +int SNACK_nfc_transmit (nfc_context *context, const char *talerPayUrl, size_t urlSize); -int nfc_connect_target (nfc_device *pnd, nfc_target *nt); +int SNACK_nfc_connect_target (nfc_device *pnd, nfc_target *nt); -void nfc_display_target_uid (nfc_target *nt); diff --git a/src/product.c b/src/product.c @@ -27,23 +27,25 @@ the attributes. #include <string.h> #include <jansson.h> +#include <gnunet/platform.h> +#include <gnunet/gnunet_util_lib.h> #include "product.h" #include "configuration.h" #include "communication.h" -void product_reset (ProductOrder *product) +void SNACK_product_reset (ProductOrder *product) { product->amount = NULL; product->product = NULL; product->paid = false; } -int product_init (ProductOrder *product, const char *currency) +int SNACK_product_init (ProductOrder *product, const char *currency) { if ( ! product ) { - printf ("product_init: product order struct must be provided\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_product_init: product order struct must be provided\n"); return EXIT_FAILURE; } @@ -52,20 +54,20 @@ int product_init (ProductOrder *product, const char *currency) product->response = malloc (sizeof(TalerResponse) ); if ( ! product->response ) { - printf ("product_init: unable to allocate memory for response struct\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_product_init: unable to allocate memory for response struct\n"); return EXIT_FAILURE; } - if ( taler_alloc_string (&(product->response->string), 1) ) + if ( SNACK_taler_alloc_string (&(product->response->string), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->orderID), 1) ) + if ( SNACK_taler_alloc_string (&(product->orderID), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->orderRequest), 1) ) + if ( SNACK_taler_alloc_string (&(product->orderRequest), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->payUrl), 1) ) + if ( SNACK_taler_alloc_string (&(product->payUrl), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->checkUrl), 1) ) + if ( SNACK_taler_alloc_string (&(product->checkUrl), 1) ) return EXIT_FAILURE; - if ( taler_alloc_string (&(product->currency), strlen (currency) + 1) ) + if ( SNACK_taler_alloc_string (&(product->currency), strlen (currency) + 1) ) return EXIT_FAILURE; strcpy (product->currency, currency); product->amount = NULL; @@ -75,7 +77,7 @@ int product_init (ProductOrder *product, const char *currency) return EXIT_SUCCESS; } -void product_exit (ProductOrder *product) +void SNACK_product_exit (ProductOrder *product) { free (product->response->string); free (product->response); @@ -89,37 +91,37 @@ void product_exit (ProductOrder *product) product->paid = false; } -int product_set_check_url (ProductOrder *product) +int SNACK_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"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR,"SNACK_prduct_set_check_url: 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); + printf("Url to check payment: %s\n\n", product->checkUrl); return EXIT_SUCCESS; } -int product_set_order_id (ProductOrder *product) +int SNACK_product_set_order_id (ProductOrder *product) { - return taler_parse_json (product->response, JSON_ORDER_ID, + return SNACK_taler_parse_json (product->response, JSON_ORDER_ID, &(product->orderID)); } -int product_set_pay_url (ProductOrder *product) +int SNACK_product_set_pay_url (ProductOrder *product) { - return taler_parse_json (product->response, JSON_PAY_URI, &(product->payUrl)); + return SNACK_taler_parse_json (product->response, JSON_PAY_URI, &(product->payUrl)); } -int product_set_paid_status (ProductOrder *product) +int SNACK_product_set_paid_status (ProductOrder *product) { json_error_t error; json_t *root = NULL; @@ -130,14 +132,14 @@ int product_set_paid_status (ProductOrder *product) root = json_loads (product->response->string, JSON_DISABLE_EOF_CHECK, &error); if ( ! root ) { - printf ("taler_parse_json: %s\n", error.text); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: %s\n", error.text); return EXIT_FAILURE; } /* extract the boolean value of paid out of the json object */ if ( json_unpack (root, "{s:b}", JSON_PAID, &b) < 0 ) { - printf ("taler_parse_json: no value \"%s\" found!\n", JSON_PAID); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_taler_parse_json: no value \"%s\" found!\n", JSON_PAID); json_decref (root); return EXIT_FAILURE; } @@ -151,7 +153,7 @@ int product_set_paid_status (ProductOrder *product) } else { - printf ("product_set_paid_status: json parsing failed\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_product_set_paid_status: json parsing failed\n"); return EXIT_FAILURE; } diff --git a/src/product.h b/src/product.h @@ -52,18 +52,18 @@ typedef struct ProductOrder } ProductOrder; -int product_init (ProductOrder *product, const char*currency); +int SNACK_product_init (ProductOrder *product, const char*currency); -void product_exit (ProductOrder *product); +void SNACK_product_exit (ProductOrder *product); -void product_reset (ProductOrder *product); +void SNACK_product_reset (ProductOrder *product); -int product_set_check_url (ProductOrder *product); +int SNACK_product_set_check_url (ProductOrder *product); -int product_set_order_id (ProductOrder *product); +int SNACK_product_set_order_id (ProductOrder *product); -int product_set_pay_url (ProductOrder *product); +int SNACK_product_set_pay_url (ProductOrder *product); -int product_set_paid_status (ProductOrder *product); +int SNACK_product_set_paid_status (ProductOrder *product); #endif // PRODUCT_H diff --git a/src/wallet.c b/src/wallet.c @@ -26,84 +26,114 @@ along with #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <jansson.h> +#include <unistd.h> +#include <gnunet/platform.h> +#include <gnunet/gnunet_util_lib.h> #include "wallet.h" #include "configuration.h" -int wallet_select_aid (nfc_device *pnd) +int SNACK_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 ( SNACK_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"); + SNACK_print_hex_info("SNACK_wallet_select_aid: Selecting Taler apk using AID", taler_aid, sizeof(taler_aid)); if ( nfc_initiator_transceive_bytes (pnd, message, size, response, sizeof(response), TRANSMIT_TIMEOUT) < 0 ) { - printf ("wallet_select_aid: Failed to select apk\n\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_wallet_select_aid: Failed to select apk\n\n"); return EXIT_FAILURE; } - return check_response (response, sizeof(response) ); + return SNACK_check_response (response, sizeof(response) ); } -int wallet_put_message (nfc_device *pnd, const char *msg, size_t msgSize) +int SNACK_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 ( SNACK_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"); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "SNACK_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"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_wallet_put_message: Failed to put message\n\n"); return EXIT_FAILURE; } - return check_response (response, sizeof(response) ); + return SNACK_check_response (response, sizeof(response) ); } +int SNACK_wallet_tunneling(nfc_device *pnd, const char *msg, size_t msgSize) +{ + uint8_t response[255] = {0x00}; + + printf( "checking for tunneling request\n"); + + if ( nfc_initiator_transceive_bytes (pnd, get_data, sizeof(get_data), response, + sizeof(response), TRANSMIT_TIMEOUT) < 0 ) -int wallet_transmit (nfc_device *pnd, const char *msg, size_t msgLen) + { + printf ("wallet_put_message: Failed to put message\n\n"); + return EXIT_FAILURE; + } + + for ( uint8_t i = 0; i < 255; ++i ) { + printf ("%.2x ", response[i]); + } + printf ("\n"); + + return SNACK_check_response (response, sizeof(response) ); + +} + +int SNACK_wallet_transmit (nfc_device *pnd, const char *msg, size_t msgLen) { if ( ! msg ) { - printf ("wallet_transmit: No message to send\n\n"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_wallet_transmit: No message to send\n"); return EXIT_FAILURE; } - if ( wallet_select_aid (pnd) ) + if ( SNACK_wallet_select_aid (pnd) ) return EXIT_FAILURE; - printf ("wallet_transmit: Taler wallet apk selected\n\n"); - if ( wallet_put_message (pnd, msg, msgLen) ) + if ( SNACK_put_message (pnd, msg, msgLen) ) return EXIT_FAILURE; - printf ("wallet_transmit: Transmitted message to taler wallet\n\n"); + + //------------------------------------------------------------------------------------------- + //sleep(3); + //while ( SNACK_wallet_tunneling(pnd, msg, msgLen) == EXIT_SUCCESS ) + // sleep(3); + // ; + //-------------------------------------------------------------------------------------------- + + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "SNACK_wallet_transmit: Transmitted message to taler wallet\n\n"); return EXIT_SUCCESS; } -int check_response (uint8_t *response, uint8_t responseLen) +int SNACK_check_response (uint8_t *response, uint8_t responseLen) { if ( strcmp ((char *) response, APDU_SUCCESS) == 0 ) { - printf ("Transmission success\n"); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Transmission success\n"); } else { - printf ("Transmission failure, return code: "); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Transmission failure, return code: "); for ( uint8_t i = 0; i < responseLen; ++i ) { printf ("%.2x ", response[i]); } @@ -114,12 +144,12 @@ 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 SNACK_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"); + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SNACK_concat_message: command and message can't be null"); return EXIT_FAILURE; } @@ -133,3 +163,16 @@ int concat_message (const uint8_t*command, size_t commandSize, const return EXIT_SUCCESS; } + +void SNACK_print_hex_info( const char* message, const uint8_t *hexArray, size_t sizeHex ){ + + char hex[5] = {""}; + char str[64] = {""}; + + for ( unsigned int i = 0; i < sizeHex; ++i ){ + sprintf (hex, "%.2x ", hexArray[i]); + strcat(str, hex); + } + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "%s: %s\n", message, str); + +} diff --git a/src/wallet.h b/src/wallet.h @@ -32,15 +32,19 @@ along with #define TRANSMIT_TIMEOUT 500 -int wallet_select_aid (nfc_device *pnd); +int SNACK_wallet_select_aid (nfc_device *pnd); -int wallet_put_message (nfc_device *pnd, const char *msg, size_t msgSize); +int SNACK_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 SNACK_wallet_tunneling(nfc_device *pnd, const char *msg, size_t msgSize); -int concat_message (const uint8_t*command, size_t commandSize, const +int SNACK_wallet_transmit (nfc_device*pnd, const char *msg, size_t msgLen); + +int SNACK_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 SNACK_check_response (uint8_t *response, uint8_t responseLen); + +void SNACK_print_hex_info( const char* message, const uint8_t *hexArray, size_t sizeHex ); #endif // WALLET_H