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

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

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

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

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

Loading…
Cancel
Save