Browse Source

fixed reader last result

master
Robin Thoni 9 years ago
parent
commit
f9dd60543a
3 changed files with 27 additions and 18 deletions
  1. 12
    7
      src/DataAccess/ScReader.cpp
  2. 6
    2
      src/include/ScReader.h
  3. 9
    9
      tests/test_pcsc_cpp_tools.cpp

+ 12
- 7
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
+thread_local LONG ScReader::_lastStaticResult = SCARD_S_SUCCESS;
12
 
12
 
13
 ScReader::~ScReader()
13
 ScReader::~ScReader()
14
 {
14
 {
20
     if (!freeContext())
20
     if (!freeContext())
21
         return false;
21
         return false;
22
     _context = new SCARDCONTEXT[1];
22
     _context = new SCARDCONTEXT[1];
23
-    _lastResult = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
24
-    return _lastResult == SCARD_S_SUCCESS;
23
+    _lastStaticResult = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
24
+    return _lastStaticResult == SCARD_S_SUCCESS;
25
 }
25
 }
26
 
26
 
27
 bool ScReader::freeContext()
27
 bool ScReader::freeContext()
28
 {
28
 {
29
     if (!_context)
29
     if (!_context)
30
         return true;
30
         return true;
31
-    _lastResult = SCardReleaseContext(*_context);
31
+    _lastStaticResult = SCardReleaseContext(*_context);
32
     delete[] _context;
32
     delete[] _context;
33
     _context = nullptr;
33
     _context = nullptr;
34
-    return _lastResult == SCARD_S_SUCCESS;
34
+    return _lastStaticResult == SCARD_S_SUCCESS;
35
 }
35
 }
36
 
36
 
37
 bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
37
 bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
75
     std::vector<std::shared_ptr<ScReader>> readers;
75
     std::vector<std::shared_ptr<ScReader>> readers;
76
     DWORD readerCount = SCARD_AUTOALLOCATE;
76
     DWORD readerCount = SCARD_AUTOALLOCATE;
77
     LPTSTR readersNames = nullptr;
77
     LPTSTR readersNames = nullptr;
78
-    _lastResult = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
79
-    if (_lastResult == SCARD_S_SUCCESS)
78
+    _lastStaticResult = SCardListReaders(*_context, nullptr, (LPTSTR)&readersNames, &readerCount);
79
+    if (_lastStaticResult == SCARD_S_SUCCESS)
80
     {
80
     {
81
         LPTSTR reader = readersNames;
81
         LPTSTR reader = readersNames;
82
         while (*reader)
82
         while (*reader)
89
     return readers;
89
     return readers;
90
 }
90
 }
91
 
91
 
92
+LONG ScReader::getLastStaticResult()
93
+{
94
+    return _lastStaticResult;
95
+}
96
+
92
 LONG ScReader::getLastResult()
97
 LONG ScReader::getLastResult()
93
 {
98
 {
94
     return _lastResult;
99
     return _lastResult;

+ 6
- 2
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();
24
+    static LONG getLastStaticResult();
25
+
26
+    LONG getLastResult();
25
 
27
 
26
     static bool establishContext();
28
     static bool establishContext();
27
 
29
 
41
 
43
 
42
     static thread_local LPSCARDCONTEXT _context;
44
     static thread_local LPSCARDCONTEXT _context;
43
 
45
 
46
+    static thread_local LONG _lastStaticResult;
47
+
44
     LPSCARDHANDLE _card;
48
     LPSCARDHANDLE _card;
45
 
49
 
46
     LPDWORD _proto;
50
     LPDWORD _proto;
47
 
51
 
48
     SCARD_IO_REQUEST _sendPci;
52
     SCARD_IO_REQUEST _sendPci;
49
 
53
 
50
-    static thread_local LONG _lastResult;
54
+    LONG _lastResult;
51
 };
55
 };
52
 
56
 
53
 # include "ScReader.hxx"
57
 # include "ScReader.hxx"

+ 9
- 9
tests/test_pcsc_cpp_tools.cpp View File

13
     } while (0)
13
     } while (0)
14
 
14
 
15
 #define TEST_byteArrayToString(STR, DATA, SIZE) \
15
 #define TEST_byteArrayToString(STR, DATA, SIZE) \
16
-  EXPECT_EQ(STR, ScHex::byteArrayToString(std::string((char*)memcpy(new BYTE[SIZE], DATA, SIZE), SIZE)))
16
+  ASSERT_EQ(STR, ScHex::byteArrayToString(std::string((char*)memcpy(new BYTE[SIZE], DATA, SIZE), SIZE)))
17
 
17
 
18
 TEST(Hex, ByteArrayToString)
18
 TEST(Hex, ByteArrayToString)
19
 {
19
 {
22
 
22
 
23
 #define TEST_stringToByteArray(STR, DATA, SIZE) do {    \
23
 #define TEST_stringToByteArray(STR, DATA, SIZE) do {    \
24
     auto bytes = ScHex::stringToByteArray(STR);         \
24
     auto bytes = ScHex::stringToByteArray(STR);         \
25
-    EXPECT_EQ(SIZE, bytes.size());                   \
26
-    EXPECT_EQ(0, memcmp(bytes.data(), DATA, SIZE));  \
25
+    ASSERT_EQ(SIZE, bytes.size());                   \
26
+    ASSERT_EQ(0, memcmp(bytes.data(), DATA, SIZE));  \
27
     } while (0)
27
     } while (0)
28
 
28
 
29
 TEST(Hex, StringToByteArray)
29
 TEST(Hex, StringToByteArray)
35
 {
35
 {
36
     std::cout << "Ensure that at lest 1 reader is connected";
36
     std::cout << "Ensure that at lest 1 reader is connected";
37
     getchar();
37
     getchar();
38
-    EXPECT_TRUE(ScReader::establishContext());
38
+    ASSERT_TRUE(ScReader::establishContext());
39
     auto readers = ScReader::getReaders();
39
     auto readers = ScReader::getReaders();
40
-    EXPECT_GE(readers.size(), 1);
40
+    ASSERT_GE(readers.size(), 1);
41
     for (auto reader : readers)
41
     for (auto reader : readers)
42
     {
42
     {
43
         std::cout << reader->getName() << std::endl;
43
         std::cout << reader->getName() << std::endl;
44
     }
44
     }
45
     auto reader = readers[0];
45
     auto reader = readers[0];
46
-    EXPECT_TRUE(reader->connect());
46
+    ASSERT_TRUE(reader->connect());
47
     auto res = reader->transmit(ScBasicCommand("94 A4 00 00 02 20 00 00"));
47
     auto res = reader->transmit(ScBasicCommand("94 A4 00 00 02 20 00 00"));
48
-    EXPECT_NE(nullptr, res->getData().data());
48
+    ASSERT_NE(nullptr, res->getData().data());
49
     auto resp = ScHex::byteArrayToString(res->getData());
49
     auto resp = ScHex::byteArrayToString(res->getData());
50
     std::cout << "Response (" << res->getData().size() << "): " << resp << std::endl;
50
     std::cout << "Response (" << res->getData().size() << "): " << resp << std::endl;
51
-    EXPECT_TRUE(reader->disconnect());
52
-    EXPECT_TRUE(ScReader::freeContext());
51
+    ASSERT_TRUE(reader->disconnect());
52
+    ASSERT_TRUE(ScReader::freeContext());
53
 }
53
 }
54
 
54
 
55
 int main(int argc, char* argv[])
55
 int main(int argc, char* argv[])

Loading…
Cancel
Save