From 65ead88908f39837172ed8ea5740752e0b2cfe68 Mon Sep 17 00:00:00 2001 From: okir Date: Fri, 3 Jan 2003 16:57:46 +0000 Subject: [PATCH] - New function connect_card() - this does all the work of connecting to the card, optionally waiting for card insertion using sc_wait_for_event git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@826 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/tools/util.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/tools/util.h | 3 ++ 2 files changed, 76 insertions(+) diff --git a/src/tools/util.c b/src/tools/util.c index 41a8abd2..8553d1d8 100644 --- a/src/tools/util.c +++ b/src/tools/util.c @@ -7,6 +7,79 @@ #include #include "util.h" +int connect_card(struct sc_context *ctx, struct sc_card **cardp, + int reader_id, int slot_id, int wait, int quiet) +{ + struct sc_reader *reader; + int r; + + if (wait) { + struct sc_reader *readers[16]; + int slots[16]; + int i, j, k, found; + unsigned int event; + + for (i = k = 0; i < ctx->reader_count; i++) { + if (reader_id >= 0 && reader_id != i) + continue; + reader = ctx->reader[i]; + for (j = 0; j < reader->slot_count; j++, k++) { + readers[k] = reader; + slots[k] = j; + } + } + + printf("Waiting for card to be inserted...\n"); + r = sc_wait_for_event(readers, slots, k, + SC_EVENT_CARD_INSERTED, + &found, &event, -1); + if (r < 0) { + fprintf(stderr, + "Error while waiting for card: %s\n", + sc_strerror(r)); + return 3; + } + + reader = readers[found]; + slot_id = slots[found]; + } else { + if (reader_id < 0) + reader_id = 0; + if (ctx->reader_count) { + fprintf(stderr, + "No smart card readers configured.\n"); + return 1; + } + if (reader_id >= ctx->reader_count) { + fprintf(stderr, + "Illegal reader number. " + "Only %d reader(s) configured.\n", + ctx->reader_count); + return 1; + } + + reader = ctx->reader[reader_id]; + slot_id = 0; + if (sc_detect_card_presence(reader, 0) != 1) { + fprintf(stderr, "Card not present.\n"); + return 3; + } + } + + if (!quiet) { + fprintf(stderr, "Connecting to card in reader %s...\n", + reader->name); + } + if ((r = sc_connect_card(reader, slot_id, cardp)) < 0) { + fprintf(stderr, + "Failed to connect to card: %s\n", + sc_strerror(r)); + return 1; + } + + return 0; +} + void print_binary(FILE *f, const u8 *buf, int count) { int i; diff --git a/src/tools/util.h b/src/tools/util.h index 093dca88..8f0bf662 100644 --- a/src/tools/util.h +++ b/src/tools/util.h @@ -37,6 +37,9 @@ const char * acl_to_str(const struct sc_acl_entry *e); void warn(const char *fmt, ...); void error(const char *fmt, ...); void fatal(const char *fmt, ...); +/* All singing all dancing card connect routine */ +int connect_card(struct sc_context *, struct sc_card **, + int reader_id, int slot_id, int wait, int quiet); #ifdef __cplusplus }