Просмотр исходного кода

fixed reader last result

master
Robin Thoni 9 лет назад
Родитель
Сommit
f9dd60543a
3 измененных файлов: 27 добавлений и 18 удалений
  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 Просмотреть файл

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

+ 6
- 2
src/include/ScReader.h Просмотреть файл

@@ -21,7 +21,9 @@ public:
21 21
 
22 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 28
     static bool establishContext();
27 29
 
@@ -41,13 +43,15 @@ private:
41 43
 
42 44
     static thread_local LPSCARDCONTEXT _context;
43 45
 
46
+    static thread_local LONG _lastStaticResult;
47
+
44 48
     LPSCARDHANDLE _card;
45 49
 
46 50
     LPDWORD _proto;
47 51
 
48 52
     SCARD_IO_REQUEST _sendPci;
49 53
 
50
-    static thread_local LONG _lastResult;
54
+    LONG _lastResult;
51 55
 };
52 56
 
53 57
 # include "ScReader.hxx"

+ 9
- 9
tests/test_pcsc_cpp_tools.cpp Просмотреть файл

@@ -13,7 +13,7 @@
13 13
     } while (0)
14 14
 
15 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 18
 TEST(Hex, ByteArrayToString)
19 19
 {
@@ -22,8 +22,8 @@ TEST(Hex, ByteArrayToString)
22 22
 
23 23
 #define TEST_stringToByteArray(STR, DATA, SIZE) do {    \
24 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 27
     } while (0)
28 28
 
29 29
 TEST(Hex, StringToByteArray)
@@ -35,21 +35,21 @@ TEST(Reader, Test1)
35 35
 {
36 36
     std::cout << "Ensure that at lest 1 reader is connected";
37 37
     getchar();
38
-    EXPECT_TRUE(ScReader::establishContext());
38
+    ASSERT_TRUE(ScReader::establishContext());
39 39
     auto readers = ScReader::getReaders();
40
-    EXPECT_GE(readers.size(), 1);
40
+    ASSERT_GE(readers.size(), 1);
41 41
     for (auto reader : readers)
42 42
     {
43 43
         std::cout << reader->getName() << std::endl;
44 44
     }
45 45
     auto reader = readers[0];
46
-    EXPECT_TRUE(reader->connect());
46
+    ASSERT_TRUE(reader->connect());
47 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 49
     auto resp = ScHex::byteArrayToString(res->getData());
50 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 55
 int main(int argc, char* argv[])

Загрузка…
Отмена
Сохранить