taler-digitizer.c (7267B)
1 #include <gnunet/gnunet_util_lib.h> 2 #include "taler_digitizer_util.h" 3 #include "lib/bank_api_get_config.h" 4 5 6 /** 7 * Time unit for PERSON_WITHDRAL_PERIOD config. 8 * Normal in Days but can be changed for testing 9 */ 10 #define DIGITIZER_PERSON_WITHDRAWL_PERIOD_TIME_UNIT GNUNET_TIME_UNIT_DAYS 11 12 /** 13 * Global return value 14 */ 15 static int global_ret; 16 17 /** 18 * Global option '-d' to enable diagnostics set. 19 */ 20 static int enable_diagnostics; 21 22 /** 23 * Taler Backend url read from configuration file 24 */ 25 static char *cfg_backend_base_url; 26 27 /** 28 * Currency read from configuration file 29 */ 30 static char *cfg_currency; 31 32 /** 33 * Minimum balance read from configuration file 34 */ 35 static unsigned long long cfg_backend_min_balance; 36 37 /** 38 * Per-person withdrawal limit read from configuration file 39 */ 40 static unsigned long long cfg_person_withdrawllimit; 41 42 /** 43 * Per-person withdrawal period read from configuration file 44 */ 45 static struct GNUNET_TIME_Relative cfg_person_withdrawl_period; 46 47 /** 48 * KYC functionality flag read from configuration file 49 */ 50 static enum GNUNET_GenericReturnValue cfg_kyc_functionality; 51 52 /** 53 * Handle to the context for interacting with the bank. 54 */ 55 static struct GNUNET_CURL_Context *curl_ctx; 56 57 /** 58 * Scheduler context for running the @e ctx. 59 */ 60 static struct GNUNET_CURL_RescheduleContext *reschedule_ctx; 61 62 /** 63 * Handle for get_config request 64 */ 65 static struct TALER_BANK_GetConfigHandle *get_config_handle; 66 67 68 static void 69 on_config_received (void *cls, 70 const struct TALER_BANK_ConfigResponse *vr) 71 { 72 (void) cls; 73 (void) vr; 74 75 get_config_handle = NULL; 76 77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 78 "Config Callback\n"); 79 80 GNUNET_SCHEDULER_shutdown (); 81 } 82 83 84 /** 85 * @brief Cleanup when no task is scheduled anymore 86 * 87 * @param cls closure 88 */ 89 static void 90 shutdown_task (void *cls) 91 { 92 (void)cls; 93 94 if (NULL != get_config_handle) 95 { 96 TALER_BANK_get_config_cancel (get_config_handle); 97 get_config_handle = NULL; 98 } 99 if (NULL != reschedule_ctx) 100 { 101 GNUNET_CURL_gnunet_rc_destroy (reschedule_ctx); 102 reschedule_ctx = NULL; 103 } 104 if (NULL != curl_ctx) 105 { 106 GNUNET_CURL_fini (curl_ctx); 107 curl_ctx = NULL; 108 } 109 } 110 111 112 /** 113 * @brief Start the application 114 * 115 * @param cls closure 116 * @param args arguments left 117 * @param cfgfile config file name 118 * @param cfg handle for the configuration 119 */ 120 static void 121 run (void *cls, 122 char *const *args, 123 const char *cfgfile, 124 const struct GNUNET_CONFIGURATION_Handle *cfg) 125 { 126 (void) cls; 127 (void) args; 128 (void) cfgfile; 129 130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 131 "Parse Configuration\n"); 132 133 if (GNUNET_OK != 134 GNUNET_CONFIGURATION_get_value_string (cfg, 135 "taler-digitizer", 136 "BACKEND_BASE_URL", 137 &cfg_backend_base_url)) 138 { 139 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 140 "taler-digitizer", 141 "BACKEND_BASE_URL"); 142 global_ret = EXIT_FAILURE; 143 return; 144 } 145 if (GNUNET_OK != 146 GNUNET_CONFIGURATION_get_value_string (cfg, 147 "taler-digitizer", 148 "CURRENCY", 149 &cfg_currency)) 150 { 151 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 152 "taler-digitizer", 153 "CURRENCY"); 154 global_ret = EXIT_FAILURE; 155 return; 156 } 157 if (GNUNET_OK != 158 GNUNET_CONFIGURATION_get_value_number (cfg, 159 "taler-digitizer", 160 "BANK_MIN_BALANCE", 161 &cfg_backend_min_balance)) 162 { 163 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 164 "taler-digitizer", 165 "BANK_MIN_BALANCE"); 166 global_ret = EXIT_FAILURE; 167 return; 168 } 169 if (GNUNET_OK != 170 GNUNET_CONFIGURATION_get_value_number (cfg, 171 "taler-digitizer", 172 "PERSON_WITHDRAWLLIMIT", 173 &cfg_person_withdrawllimit)) 174 { 175 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 176 "taler-digitizer", 177 "PERSON_WITHDRAWLLIMIT"); 178 global_ret = EXIT_FAILURE; 179 return; 180 } 181 182 unsigned long long person_withdrawl_period_number; 183 if (GNUNET_OK != 184 GNUNET_CONFIGURATION_get_value_number (cfg, 185 "taler-digitizer", 186 "PERSON_WITHDRAL_PERIOD", 187 &person_withdrawl_period_number)) 188 { 189 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 190 "taler-digitizer", 191 "PERSON_WITHDRAL_PERIOD"); 192 global_ret = EXIT_FAILURE; 193 return; 194 } 195 cfg_person_withdrawl_period = GNUNET_TIME_relative_multiply ( 196 DIGITIZER_PERSON_WITHDRAWL_PERIOD_TIME_UNIT, 197 person_withdrawl_period_number); 198 199 cfg_kyc_functionality = GNUNET_CONFIGURATION_get_value_yesno (cfg, 200 "taler-digitizer", 201 "KYC_FUNCTIONALITY"); 202 if (GNUNET_SYSERR == cfg_kyc_functionality) 203 { 204 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 205 "taler-digitizer", 206 "KYC_FUNCTIONALITY"); 207 global_ret = EXIT_FAILURE; 208 return; 209 } 210 211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 212 "Config URL: %s\n", 213 cfg_backend_base_url); 214 215 curl_ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule, 216 &reschedule_ctx); 217 reschedule_ctx = GNUNET_CURL_gnunet_rc_create (curl_ctx); 218 219 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 220 NULL); 221 222 get_config_handle = TALER_BANK_get_config (curl_ctx, 223 cfg_backend_base_url, 224 &on_config_received, 225 NULL); 226 227 228 return; 229 } 230 231 232 int 233 main (int argc, 234 char *const *argv) 235 { 236 int ret; 237 238 struct GNUNET_GETOPT_CommandLineOption options[] = { 239 GNUNET_GETOPT_option_flag ('d', 240 "enable-diagnostics", 241 "enable diagnostics for debuging", 242 &enable_diagnostics), 243 GNUNET_GETOPT_OPTION_END 244 }; 245 246 ret = GNUNET_PROGRAM_run (TALER_DIGITIZER_project_data (), 247 argc, 248 argv, 249 "taler-digitizer", 250 "This is an application for the Cash Digitizer." 251 " It accepts cash and transfers it to the Taler Wallet.\n", 252 options, 253 &run, 254 NULL); 255 256 if (GNUNET_NO == ret) 257 return 0; 258 if (GNUNET_OK != ret) 259 return 1; 260 return global_ret; 261 }