14#include "keyple/plugin/pcsc/cpp/CardTerminals.hpp"
22#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
23#include "keyple/plugin/pcsc/cpp/exception/CardException.hpp"
24#include "keyple/plugin/pcsc/cpp/exception/CardTerminalException.hpp"
31using keyple::core::util::cpp::exception::IllegalArgumentException;
32using keyple::plugin::pcsc::cpp::exception::CardException;
33using keyple::plugin::pcsc::cpp::exception::CardTerminalException;
35CardTerminals::CardTerminals(SCARDCONTEXT& context)
41CardTerminals::waitForChange()
47CardTerminals::waitForChange(
long timeout)
50 throw IllegalArgumentException(
51 "Negative timeout " + std::to_string(timeout));
52 }
else if (timeout == 0) {
56 mZombieReaders.clear();
58 for (
auto& reader: mKnownReaders) {
59 reader.dwCurrentState = reader.dwEventState;
60 reader.dwEventState = 0;
63 LONG rv = SCardGetStatusChange(
64 mContext, timeout, mKnownReaders.data(),
static_cast<DWORD
>(mKnownReaders.size()));
65 if (rv ==
static_cast<LONG
>(SCARD_E_TIMEOUT)) {
72std::shared_ptr<CardTerminal>
73CardTerminals::getTerminal(
const std::string& name)
76 for (
const auto& terminal : list()) {
77 if (terminal->getName() == name) {
84 }
catch (
const CardException&) {
89const std::vector<std::shared_ptr<CardTerminal>>
92 return list(State::ALL);
96const std::vector<std::shared_ptr<CardTerminal>>
97CardTerminals::list(
const State )
100 char* readers = NULL;
103 static std::vector<std::shared_ptr<CardTerminal>> list;
108 ret = SCardListReaders(mContext, NULL, NULL, &len);
109 if (ret != SCARD_S_SUCCESS) {
110 throw CardTerminalException(pcsc_stringify_error(ret));
113 readers =
static_cast<char*
>(calloc(len,
sizeof(
char)));
115 if (readers == NULL) {
120 ret = SCardListReaders(mContext, NULL, readers, &len);
121 if (ret != SCARD_S_SUCCESS) {
122 throw CardTerminalException(pcsc_stringify_error(ret));
133 auto terminal = std::make_shared<CardTerminal>(shared_from_this(), s);
134 list.push_back(terminal);
135 ptr += strlen(ptr) + 1;