diff --git a/srsue/hdr/upper/usim_base.h b/srsue/hdr/upper/usim_base.h index e8583bd46..9af01b8b6 100644 --- a/srsue/hdr/upper/usim_base.h +++ b/srsue/hdr/upper/usim_base.h @@ -48,6 +48,7 @@ typedef struct{ std::string imei; std::string k; std::string pin; + std::string reader; }usim_args_t; class usim_base diff --git a/srsue/src/main.cc b/srsue/src/main.cc index 1825fc721..85aacc330 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -133,6 +133,7 @@ void parse_args(all_args_t *args, int argc, char *argv[]) { ("usim.imei", bpo::value(&args->usim.imei), "USIM IMEI") ("usim.k", bpo::value(&args->usim.k), "USIM K") ("usim.pin", bpo::value(&args->usim.pin), "PIN in case real SIM card is used") + ("usim.reader", bpo::value(&args->usim.reader)->default_value(""), "Force specifiy PCSC reader. Default: Try all available readers.") /* Expert section */ ("expert.ip_netmask", diff --git a/srsue/src/upper/pcsc_usim.cc b/srsue/src/upper/pcsc_usim.cc index 26b7f8a1a..d94bebe1a 100644 --- a/srsue/src/upper/pcsc_usim.cc +++ b/srsue/src/upper/pcsc_usim.cc @@ -28,6 +28,7 @@ #include #include #include "srslte/common/bcd_helpers.h" +#include "string.h" #define CHECK_SIM_PIN 1 @@ -395,7 +396,8 @@ void pcsc_usim::generate_as_keys_ho(uint32_t pci, int pcsc_usim::scard::init(usim_args_t *args, srslte::log *log_) { int ret_value = SRSLTE_ERROR; - int pos = 0; // SC reader + uint pos = 0; // SC reader + bool reader_found = false; //int transaction = 1; size_t blen; log = log_; @@ -430,19 +432,72 @@ int pcsc_usim::scard::init(usim_args_t *args, srslte::log *log_) return ret_value; } - log->info("%s\n", readers); - // TODO: Implement reader selection - // (readers is a list of available readers. The last entry is terminated with double null) - pos = 0; // select first reader + /* readers: NULL-separated list of reader names, and terminating NULL */ + pos = 0; + while (pos < len-1) { + log->info("Available Card Reader: %s\n", &readers[pos]); + while (readers[pos] != '\0' && pos < len) { + pos++; + } + pos++; // skip separator + } + + reader_found = false; + pos = 0; + + // If no reader specified, test all available readers for SIM cards. Otherwise consider specified reader only. + if (args->reader.length() == 0) { + while (pos < len && !reader_found) { + log->info("Trying Card Reader: %s\n", &readers[pos]); + // Connect to card + ret = SCardConnect(scard_context, &readers[pos], SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &scard_handle, &scard_protocol); + if (ret == SCARD_S_SUCCESS) { + reader_found = true; + } else { + if (ret == (long)SCARD_E_NO_SMARTCARD) { + log->error("No smart card inserted.\n"); + } else { + log->error("%s\n", pcsc_stringify_error(ret)); + } + log->info("Failed to use Card Reader: %s\n", &readers[pos]); - // Connect to reader - ret = SCardConnect(scard_context, &readers[pos], SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &scard_handle, &scard_protocol); - if (ret != SCARD_S_SUCCESS) { - if (ret == (long)SCARD_E_NO_SMARTCARD) { - log->error("No smart card inserted.\n"); + // proceed to next reader + while (pos < len && readers[pos] != '\0' ) { + pos++; + } + pos++; // skip separator + } + } + } else { + // card reader specified in config. search for this reader. + while (pos < len && !reader_found) { + if (strcmp(&readers[pos], args->reader.c_str()) == 0) { + reader_found = true; + log->info("Card Reader found: %s\n", args->reader.c_str()); + } else { + // next reader + while (pos < len && readers[pos] != '\0' ) { + pos++; + } + pos++; // skip separator + } + } + if (!reader_found) { + log->error("Cannot find reader: %s\n", args->reader.c_str()); } else { - log->error("%s\n", pcsc_stringify_error(ret)); + ret = SCardConnect(scard_context, &readers[pos], SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &scard_handle, &scard_protocol); + if (ret == SCARD_S_SUCCESS) { + // successfully connected to card + } else { + if (ret == (long)SCARD_E_NO_SMARTCARD) { + log->error("No smart card inserted.\n"); + } else { + log->error("%s\n", pcsc_stringify_error(ret)); + } + + log->info("Failed to use Card Reader: %s\n", args->reader.c_str()); + } } } diff --git a/srsue/ue.conf.example b/srsue/ue.conf.example index 43b5e1e7d..746df2192 100644 --- a/srsue/ue.conf.example +++ b/srsue/ue.conf.example @@ -93,6 +93,7 @@ file_max_size = -1 # imsi: 15 digit International Mobile Subscriber Identity # imei: 15 digit International Mobile Station Equipment Identity # pin: PIN in case real SIM card is used +# reader: Specify card reader by it's name as listed by 'pcsc_scan'. If empty, try all available readers. ##################################################################### [usim] mode = soft @@ -101,6 +102,7 @@ op = 63BFA50EE6523365FF14C1F45F88737D k = 00112233445566778899aabbccddeeff imsi = 001010123456789 imei = 353490069873319 +#reader = #pin = 1234 #####################################################################