taler-mdb

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

commit a02a3a4b5b706e23c2f8cb2f34c463c5a1800733
parent ae2056f23daf908868c8f501351a2d454537844d
Author: BOSS_Marco <bossm8@students.bfh.ch>
Date:   Thu, 31 Oct 2019 14:03:38 +0100

json parsing and creating now done with libjannson

Diffstat:
Msrc/communication.c | 48++++++++++++++++++++++--------------------------
Msrc/communication.h | 2+-
Msrc/configuration.h | 15+++++++++++++++
Msrc/main.c | 7+++++++
Msrc/product.c | 42+++++++++++++++++++++++++++++-------------
Msrc/wallet.c | 1+
Msrc/wallet.h | 12------------
7 files changed, 75 insertions(+), 52 deletions(-)

diff --git a/src/communication.c b/src/communication.c @@ -90,12 +90,12 @@ int taler_create_order_req( ProductOrder *product ) char amountStr[ strlen(product->currency) + strlen(product->amount) + 2 ]; sprintf( amountStr, "%s:%s", product->currency, product->amount ); - // create the fullfillment ur, e.g. "taler://fulfillment-success/Enjoy+your+ice+cream!"; ( +2 because of '!' and '\0' ) - char fullfillmentUrl[ strlen(FULLFILLMENT_URL) + strlen(FULLFILLMENT_MSG) + strlen(product->product) + 2 ]; - sprintf( fullfillmentUrl, "%s%s%s!", FULLFILLMENT_URL, FULLFILLMENT_MSG, product->product ); + // create the fulfillment ur, e.g. "taler://fulfillment-success/Enjoy+your+ice+cream!"; ( +2 because of '!' and '\0' ) + char fulfillmentUrl[ strlen(FULLFILLMENT_URL) + strlen(FULLFILLMENT_MSG) + strlen(product->product) + 2 ]; + sprintf( fulfillmentUrl, "%s%s%s!", FULLFILLMENT_URL, FULLFILLMENT_MSG, product->product ); // create the json object for the order request - json_t *orderReq = json_pack( "{ s: { s:s, s:s, s:s }}", "order", "summary", product->product, "amount", amountStr, "fullfilment_url", fullfillmentUrl ); + json_t *orderReq = json_pack( "{ s: { s:s, s:s, s:s }}", "order", "summary", product->product, "amount", amountStr, "fulfillment_url", fulfillmentUrl ); if( !orderReq ){ printf( "taler_create_order_req: error creating json object\n" ); return EXIT_FAILURE; @@ -120,6 +120,7 @@ int taler_create_order_req( ProductOrder *product ) // print the created order request and free the json object printf( "Created order Request: \n" ); json_dumpf( orderReq, stdout, JSON_INDENT(2) ); + printf("\n"); json_delete( orderReq ); // free the allocated buffer from json_dumps @@ -164,6 +165,9 @@ int taler_create_order( CURL *curl, ProductOrder *product ) // clean up curl_slist_free_all( list ); + + printf("%s\n", product->response->string ); + return EXIT_SUCCESS; } @@ -203,48 +207,40 @@ 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 ) { if( !(*returnStr) ) taler_alloc_string( returnStr, 1 ); - char *result = NULL; - char *temp = NULL; - char mbr[64]; json_error_t error; - json_t *root = json_loads( response->string, JSON_DISABLE_EOF_CHECK, &error ); + json_t *root = NULL; + root = json_loads( response->string, JSON_DISABLE_EOF_CHECK, &error ); if( !root ){ printf("taler_parse_json: %s\n", error.text ); return EXIT_FAILURE; } - 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, "\""); - } - } + + char *result = NULL; + if( json_unpack( root, "{s:s}", memberName, &result ) < 0 ){ + printf( "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\n", memberName ); + printf( "taler_parse_json: no member named \"%s\" found!\n", memberName ); + json_decref( root ); return EXIT_FAILURE; } + char *temp = NULL; temp = realloc( *returnStr, strlen(result) + 1 ); if( !temp ){ printf( "taler_parse_json: could not allocate memory for member\n" ); + json_decref( root ); return EXIT_FAILURE; } *returnStr = temp; strcpy( *returnStr, result ); + json_decref( root ); return EXIT_SUCCESS; } diff --git a/src/communication.h b/src/communication.h @@ -47,6 +47,6 @@ int taler_create_order (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); + char **returnStr); #endif // COMM_H diff --git a/src/configuration.h b/src/configuration.h @@ -26,6 +26,9 @@ along with #ifndef URL_H #define URL_H +#include <stdio.h> +#include <inttypes.h> + static const char* const ORDER = "/order"; static const char* const CHECK = "/check-payment"; @@ -41,4 +44,16 @@ 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"; + +// 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 }; + +// 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 }; + #endif // URL_H diff --git a/src/main.c b/src/main.c @@ -151,11 +151,18 @@ int main ( ) } printf ("Order no.: %s paid!\n\n", product.orderID); + + // ----------------- + // Here comes the code for releasing the product + // ----------------- + + // 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 ) { diff --git a/src/product.c b/src/product.c @@ -26,6 +26,7 @@ the attributes. */ #include <string.h> +#include <jansson.h> #include "product.h" #include "configuration.h" @@ -109,26 +110,41 @@ int product_set_check_url (ProductOrder *product) int product_set_order_id (ProductOrder *product) { return taler_parse_json (product->response, JSON_ORDER_ID, - &(product->orderID), false); + &(product->orderID)); } 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)); } 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; - } + json_error_t error; + json_t *root = NULL; + root = json_loads( product->response->string, JSON_DISABLE_EOF_CHECK, &error ); + if( !root ){ + printf("taler_parse_json: %s\n", error.text ); + return EXIT_FAILURE; + } + + // extract the boolean value of paid out of the json object + int b = -1; + if ( json_unpack( root, "{s:b}", JSON_PAID, &b ) < 0 ){ + printf("taler_parse_json: no value \"%s\" found!\n", JSON_PAID ); + json_decref( root ); + return EXIT_FAILURE; + } + + json_decref(root); + + // set the paid status to true or false + if( b == true || b == false ){ + product->paid = b; + }else{ + printf( "product_set_paid_status: json parsing failed\n" ); + return EXIT_FAILURE; + } - return EXIT_FAILURE; + return EXIT_SUCCESS; } diff --git a/src/wallet.c b/src/wallet.c @@ -28,6 +28,7 @@ along with #include <string.h> #include "wallet.h" +#include "configuration.h" int wallet_select_aid (nfc_device *pnd) diff --git a/src/wallet.h b/src/wallet.h @@ -29,20 +29,8 @@ along with #include <nfc/nfc.h> -// 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 }; - #define TRANSMIT_TIMEOUT 500 -// 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 }; - - int wallet_select_aid (nfc_device *pnd);