Browse Source

reader last result

master
Robin Thoni 9 years ago
parent
commit
e12fe36dcf
3 changed files with 21 additions and 14 deletions
  1. 18
    12
      src/DataAccess/ScReader.cpp
  2. 3
    1
      src/include/ScReader.h
  3. 0
    1
      src/include/ScReader.hxx

+ 18
- 12
src/DataAccess/ScReader.cpp View File

@@ -8,6 +8,7 @@
8 8
 #include "ScReader.h"
9 9
 
10 10
 thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
11
+thread_local LONG ScReader::_lastResult = SCARD_S_SUCCESS;
11 12
 
12 13
 ScReader::~ScReader()
13 14
 {
@@ -19,18 +20,18 @@ bool ScReader::establishContext()
19 20
     if (!freeContext())
20 21
         return false;
21 22
     _context = new SCARDCONTEXT[1];
22
-    auto res = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
23
-    return res == SCARD_S_SUCCESS;
23
+    _lastResult = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
24
+    return _lastResult == SCARD_S_SUCCESS;
24 25
 }
25 26
 
26 27
 bool ScReader::freeContext()
27 28
 {
28 29
     if (!_context)
29 30
         return true;
30
-    auto res = SCardReleaseContext(*_context);
31
+    _lastResult = SCardReleaseContext(*_context);
31 32
     delete[] _context;
32 33
     _context = nullptr;
33
-    return res == SCARD_S_SUCCESS;
34
+    return _lastResult == SCARD_S_SUCCESS;
34 35
 }
35 36
 
36 37
 bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
@@ -39,32 +40,32 @@ bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
39 40
         return false;
40 41
     _card = new SCARDCONTEXT[1];
41 42
     _proto = new DWORD[1];
42
-    auto res = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
43
+    _lastResult = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
43 44
     if (*_proto == SCARD_PROTOCOL_T0)
44 45
         _sendPci = *SCARD_PCI_T0;
45 46
     if (*_proto == SCARD_PROTOCOL_T1)
46 47
         _sendPci = *SCARD_PCI_T1;
47
-    return res == SCARD_S_SUCCESS;
48
+    return _lastResult == SCARD_S_SUCCESS;
48 49
 }
49 50
 
50 51
 bool ScReader::disconnect(DWORD disposition)
51 52
 {
52 53
     if (!_card)
53 54
         return true;
54
-    auto res = SCardDisconnect(*_card, disposition);
55
+    _lastResult = SCardDisconnect(*_card, disposition);
55 56
     delete[] _proto;
56 57
     _proto = nullptr;
57 58
     delete[] _card;
58 59
     _card = nullptr;
59
-    return res == SCARD_S_SUCCESS;
60
+    return _lastResult == SCARD_S_SUCCESS;
60 61
 }
61 62
 
62 63
 std::shared_ptr<ScResult> ScReader::transmit(const ScCommand& command, DWORD size)
63 64
 {
64 65
     auto data = command.getData();
65 66
     auto resBuffer = new BYTE[size];
66
-    auto res = SCardTransmit(*_card, &_sendPci, (BYTE*)data.data(), data.size(), nullptr, resBuffer, &size);
67
-    if (res == SCARD_S_SUCCESS)
67
+    _lastResult = SCardTransmit(*_card, &_sendPci, (BYTE*)data.data(), data.size(), nullptr, resBuffer, &size);
68
+    if (_lastResult == SCARD_S_SUCCESS)
68 69
         return std::make_shared<ScResult>(std::string((char*)resBuffer, size));
69 70
     return std::make_shared<ScResult>("");
70 71
 }
@@ -74,8 +75,8 @@ std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()
74 75
     std::vector<std::shared_ptr<ScReader>> readers;
75 76
     DWORD readerCount = SCARD_AUTOALLOCATE;
76 77
     LPTSTR readersNames = nullptr;
77
-    auto res = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
78
-    if (res == SCARD_S_SUCCESS)
78
+    _lastResult = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
79
+    if (_lastResult == SCARD_S_SUCCESS)
79 80
     {
80 81
         LPTSTR reader = readersNames;
81 82
         while (*reader)
@@ -87,3 +88,8 @@ std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()
87 88
     SCardFreeMemory(*_context, readersNames);
88 89
     return readers;
89 90
 }
91
+
92
+LONG ScReader::getLastResult()
93
+{
94
+    return _lastResult;
95
+}

+ 3
- 1
src/include/ScReader.h View File

@@ -21,6 +21,8 @@ public:
21 21
 
22 22
     static std::vector<std::shared_ptr<ScReader>> getReaders();
23 23
 
24
+    static LONG getLastResult();
25
+
24 26
     static bool establishContext();
25 27
 
26 28
     static bool freeContext();
@@ -45,7 +47,7 @@ private:
45 47
 
46 48
     SCARD_IO_REQUEST _sendPci;
47 49
 
48
-    LONG _lastResult;
50
+    static thread_local LONG _lastResult;
49 51
 };
50 52
 
51 53
 # include "ScReader.hxx"

+ 0
- 1
src/include/ScReader.hxx View File

@@ -6,7 +6,6 @@ ScReader::ScReader(std::string name)
6 6
     : _name(name)
7 7
     , _card(nullptr)
8 8
     , _proto(nullptr)
9
-    , _lastResult(SCARD_S_SUCCESS)
10 9
 {
11 10
 }
12 11
 

Loading…
Cancel
Save