#include #include #ifdef HAVE_UNISTD_H #include #endif #include #include #include #include #include "libopensc/opensc.h" #include "libopensc/cardctl.h" #include "util.h" /* type for associations of IDs to names */ typedef struct _id2str { unsigned int id; const char *str; } id2str_t; static const char *app_name = "opensc-tool"; static int opt_wait = 0; static char ** opt_apdus; static char *opt_reader; static int opt_apdu_count = 0; static int verbose = 0; enum { OPT_SERIAL = 0x100, OPT_LIST_ALG }; static sc_context_t *ctx = NULL; static sc_card_t *card = NULL; int CardConnect (void) { int err = 0, r, c, long_optind = 0; const char *opt_driver = NULL; const char *opt_conf_entry = NULL; sc_context_param_t ctx_param; setbuf(stderr, NULL); setbuf(stdout, NULL); memset(&ctx_param, 0, sizeof(ctx_param)); ctx_param.ver = 0; ctx_param.app_name = app_name; r = sc_context_create(&ctx, &ctx_param); if (r) { //fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); return 1; } err = util_connect_card(ctx, &card, opt_reader, opt_wait, verbose); if (err) { CardDisconnect(); return 2; } return 0; } char* CardGetName(void) { printf("Card name: "); printf("%s\n", card->name); return card->name; } int CardDisconnect(void) { if (card) { sc_unlock(card); sc_disconnect_card(card); card = NULL; } if (ctx) { sc_release_context(ctx); ctx = NULL; } return 0; }