From 2ea4dd4c0e45189e9d11cf3726c384780db4f66a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Tue, 18 Mar 2014 11:48:26 +0000 Subject: [PATCH] new method reset for pcsc connections; refs #29 --- src/pcsc.hxx | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/pcsc.hxx b/src/pcsc.hxx index 99e66d0..da92c2e 100644 --- a/src/pcsc.hxx +++ b/src/pcsc.hxx @@ -551,6 +551,27 @@ namespace pcsc { // void close(const std::string& s) { // } + + /// Reset and reestablish the connection. + /** @note You must remove all readers that have been returned by + @ref reader(), if you have assigned them to a variable + and stored them. Otherwise the connections will remain + open and next access should probably fail. + @begincode + { + mrw::Shared r(c.reader(name)); + // use r + c.reset(); // now r is invalid + // either reassign r: + r = c.reader(name); + // or simply reset it + r.reset(); + } + // or clenaup by leaving the context of r + @endcode */ + void reset() { + _connectionlifetime->reset(); + } //! Find all readers with a given ATR. /*! @param atr full or partial ATR to match to the reader's ATR @@ -630,7 +651,7 @@ namespace pcsc { public: //! opens connection that is closed on destruction ConnectionLifetime(Scope s, bool exc): - _exc(exc), _id(0), + _exc(exc), _id(0), _s(s), _state(SCardEstablishContext(s, 0, 0, &_id)) { CRYPTOLOG("Open Connection"); check("establish smartcard context"); @@ -1064,10 +1085,20 @@ namespace pcsc { return _readers.find(name)->second; } + /// Cleans up the readers, resets the context. + void reset() { + _readers.clear(); + _state = SCardReleaseContext(_id); + check("smartcard release context"); + _state = SCardEstablishContext(_s, 0, 0, &_id); + check("establish smartcard context"); + } + private: bool _exc; SCARDCONTEXT _id; + Scope _s; long _state; //! Readers are closed when the last shared object is destructed