Browse Source

added business

develop
Robin Thoni 8 years ago
parent
commit
a998d5e54b

+ 16
- 14
cli/Interface/MainClass.cpp View File

4
 
4
 
5
 #include <iostream>
5
 #include <iostream>
6
 #include <sysexits.h>
6
 #include <sysexits.h>
7
-#include <Business/StringUtils.h>
7
+#include <DBO/StringUtils.h>
8
+#include <Business/FreeFareDeviceBusiness.h>
8
 #include "DBO/Result.h"
9
 #include "DBO/Result.h"
9
-#include "Business/LibNfc.h"
10
-#include "Business/FreeFareDevice.h"
10
+#include "Business/LibNfcBusiness.h"
11
+#include "Business/FreeFareDeviceBusiness.h"
11
 #include "CommandLineParser.h"
12
 #include "CommandLineParser.h"
12
 #include "MainClass.h"
13
 #include "MainClass.h"
13
 
14
 
19
 
20
 
20
 int MainClass::main()
21
 int MainClass::main()
21
 {
22
 {
22
-    std::cout << "LibNfc version: " << LibNfc::getVersion() << std::endl;
23
+    std::cout << "LibNfc version: " << LibNfcBusiness::getVersion() << std::endl;
23
 
24
 
24
-    LibNfc libNfc;
25
+    LibNfcBusiness libNfc;
25
     auto init = libNfc.init();
26
     auto init = libNfc.init();
26
     if (!init) {
27
     if (!init) {
27
         init.print();
28
         init.print();
28
         return 1;
29
         return 1;
29
     }
30
     }
30
 
31
 
31
-    auto devices = libNfc.getDevices();
32
-    if (!devices) {
33
-        devices.print();
32
+    auto devicesResult = libNfc.getDevices();
33
+    if (!devicesResult) {
34
+        devicesResult.print();
34
         return 2;
35
         return 2;
35
     }
36
     }
36
-    if (devices.getData().size() == 0) {
37
+    auto devices = devicesResult.getData();
38
+    if (devices.size() == 0) {
37
         std::cerr << "No NFC device found" << std::endl;
39
         std::cerr << "No NFC device found" << std::endl;
38
         return 3;
40
         return 3;
39
     }
41
     }
40
 
42
 
41
-    std::cout << "Found " << devices.getData().size() << " devices: " << std::endl;
42
-    for (size_t i = 0; i < devices.getData().size(); ++i) {
43
-        std::cout << devices.getData()[i]->getConnStr() << std::endl;
43
+    std::cout << "Found " << devices.size() << " devices: " << std::endl;
44
+    for (size_t i = 0; i < devices.size(); ++i) {
45
+        std::cout << devices[i]->getConnStr() << std::endl;
44
     }
46
     }
45
 
47
 
46
-    auto device = devices.getData()[0];
48
+    auto device = devices[0];
47
     auto open = device->open();
49
     auto open = device->open();
48
     if (!open) {
50
     if (!open) {
49
         open.print();
51
         open.print();
50
         return 4;
52
         return 4;
51
     }
53
     }
52
 
54
 
53
-    FreeFareDevice freeFareDevice(device);
55
+    FreeFareDeviceBusiness freeFareDevice(device);
54
     auto tags = freeFareDevice.getTags();
56
     auto tags = freeFareDevice.getTags();
55
     if (!tags) {
57
     if (!tags) {
56
         tags.print();
58
         tags.print();

+ 28
- 0
src/Business/FreeFareDeviceBusiness.cpp View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#include "FreeFareDeviceBusiness.h"
6
+
7
+FreeFareDeviceBusiness::FreeFareDeviceBusiness(std::shared_ptr<NfcDeviceBusiness> device)
8
+    : _device(device),
9
+      _freeFareDevice(std::make_shared<FreeFareDevice>(_device->getDevice()))
10
+{
11
+}
12
+
13
+Result<std::vector<std::shared_ptr<FreeFareTagBusiness>>> FreeFareDeviceBusiness::getTags() const
14
+{
15
+    if (!_device->isOpened()) {
16
+        return Result<std::vector<std::shared_ptr<FreeFareTagBusiness>>>::error("Device is not opened");
17
+    }
18
+    auto tagsResult = _freeFareDevice->getTags();
19
+    if (!tagsResult) {
20
+        return Result<std::vector<std::shared_ptr<FreeFareTagBusiness>>>::error(tagsResult);
21
+    }
22
+    auto tags = tagsResult.getData();
23
+    std::vector<std::shared_ptr<FreeFareTagBusiness>> tagsBusiness;
24
+    for (auto tag : tags) {
25
+        tagsBusiness.push_back(std::make_shared<FreeFareTagBusiness>(tag));
26
+    }
27
+    return Result<std::vector<std::shared_ptr<FreeFareTagBusiness>>>::ok(tagsBusiness);
28
+}

+ 27
- 0
src/Business/FreeFareDeviceBusiness.h View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#ifndef MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H
6
+#define MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H
7
+
8
+
9
+#include <DataAccess/FreeFareDevice.h>
10
+#include "NfcDeviceBusiness.h"
11
+#include "FreeFareTagBusiness.h"
12
+
13
+class FreeFareDeviceBusiness
14
+{
15
+public:
16
+    FreeFareDeviceBusiness(std::shared_ptr<NfcDeviceBusiness> device);
17
+
18
+    Result<std::vector<std::shared_ptr<FreeFareTagBusiness>>> getTags() const;
19
+
20
+protected:
21
+    std::shared_ptr<NfcDeviceBusiness> _device;
22
+
23
+    std::shared_ptr<FreeFareDevice> _freeFareDevice;
24
+};
25
+
26
+
27
+#endif //MIFARE_TOOLS_FREEFAREDEVICEBUSINESS_H

+ 45
- 0
src/Business/FreeFareTagBusiness.cpp View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#include "FreeFareTagBusiness.h"
6
+
7
+FreeFareTagBusiness::FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag)
8
+    : _tag(tag)
9
+{
10
+}
11
+
12
+ResultBool FreeFareTagBusiness::authenticate(int sector, int block, std::string key, int keyType)
13
+{
14
+    return _tag->authenticate(sector, block, key, keyType);
15
+}
16
+
17
+ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string key, int keyType)
18
+{
19
+    return _tag->readBlock(sector, block, key, keyType);
20
+}
21
+
22
+ResultString FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
23
+{
24
+    std::string res;
25
+    for (int i = 0; i < 4; ++i) {
26
+        auto data = readBlock(sector, i, key, keyType);
27
+        if (data) {
28
+            res += data.getData();
29
+        }
30
+        else {
31
+            return data;
32
+        }
33
+    }
34
+    return ResultString::ok(res);
35
+}
36
+
37
+const std::string &FreeFareTagBusiness::getUid() const
38
+{
39
+    return _tag->getUid();
40
+}
41
+
42
+std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
43
+{
44
+    return _tag;
45
+}

+ 32
- 0
src/Business/FreeFareTagBusiness.h View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#ifndef MIFARE_TOOLS_FREEFARETAGBUSINESS_H
6
+#define MIFARE_TOOLS_FREEFARETAGBUSINESS_H
7
+
8
+
9
+#include <boost/shared_ptr.hpp>
10
+#include <DataAccess/FreeFareTag.h>
11
+
12
+class FreeFareTagBusiness
13
+{
14
+public:
15
+    FreeFareTagBusiness(std::shared_ptr<FreeFareTag> tag);
16
+
17
+    ResultBool authenticate(int sector, int block, std::string key, int keyType);
18
+
19
+    ResultString readBlock(int sector, int block, std::string key, int keyType);
20
+
21
+    ResultString readSector(int sector, std::string key, int keyType);
22
+
23
+    const std::string & getUid() const;
24
+
25
+    std::shared_ptr<FreeFareTag> getTag() const;
26
+
27
+protected:
28
+    std::shared_ptr<FreeFareTag> _tag;
29
+};
30
+
31
+
32
+#endif //MIFARE_TOOLS_FREEFARETAGBUSINESS_H

+ 63
- 0
src/Business/LibNfcBusiness.cpp View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#include <algorithm>
6
+#include "LibNfcBusiness.h"
7
+
8
+LibNfcBusiness::LibNfcBusiness()
9
+{
10
+    _libNfc = std::make_shared<LibNfc>();
11
+}
12
+
13
+LibNfcBusiness::~LibNfcBusiness()
14
+{
15
+    _libNfc->clean();
16
+}
17
+
18
+ResultBool LibNfcBusiness::init()
19
+{
20
+    if (isInitialized()) {
21
+        return ResultBool::error("LibNfc is already initialized");
22
+    }
23
+    return _libNfc->init();
24
+}
25
+
26
+std::string LibNfcBusiness::getVersion()
27
+{
28
+    return LibNfc::getVersion();
29
+}
30
+
31
+void LibNfcBusiness::clean()
32
+{
33
+    if (isInitialized()) {
34
+        _libNfc->clean();
35
+    }
36
+}
37
+
38
+bool LibNfcBusiness::isInitialized() const
39
+{
40
+    return _libNfc->getContext() != 0;
41
+}
42
+
43
+Result<std::vector<std::shared_ptr<NfcDeviceBusiness>>> LibNfcBusiness::getDevices() const
44
+{
45
+    if (!isInitialized()) {
46
+        return Result<std::vector<std::shared_ptr<NfcDeviceBusiness>>>::error("LibNfc is not initialized");
47
+    }
48
+    auto devicesResult = _libNfc->getDevices();
49
+    if (!devicesResult) {
50
+        return Result<std::vector<std::shared_ptr<NfcDeviceBusiness>>>::error(devicesResult);
51
+    }
52
+    auto devices = devicesResult.getData();
53
+    std::vector<std::shared_ptr<NfcDeviceBusiness>> devicesBusiness;
54
+    for (auto device : devices) {
55
+        devicesBusiness.push_back(std::make_shared<NfcDeviceBusiness>(device));
56
+    }
57
+    return Result<std::vector<std::shared_ptr<NfcDeviceBusiness>>>::ok(devicesBusiness);
58
+}
59
+
60
+std::shared_ptr<LibNfc> LibNfcBusiness::getLibNfc() const
61
+{
62
+    return _libNfc;
63
+}

+ 36
- 0
src/Business/LibNfcBusiness.h View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#ifndef MIFARE_TOOLS_LIBNFCBUSINESS_H
6
+#define MIFARE_TOOLS_LIBNFCBUSINESS_H
7
+
8
+
9
+#include <DBO/Result.h>
10
+#include <DataAccess/LibNfc.h>
11
+#include "NfcDeviceBusiness.h"
12
+
13
+class LibNfcBusiness
14
+{
15
+public:
16
+    LibNfcBusiness();
17
+    virtual ~LibNfcBusiness();
18
+
19
+    ResultBool init();
20
+
21
+    void clean();
22
+
23
+    bool isInitialized() const;
24
+
25
+    Result<std::vector<std::shared_ptr<NfcDeviceBusiness>>> getDevices() const;
26
+
27
+    std::shared_ptr<LibNfc> getLibNfc() const;
28
+
29
+    static std::string getVersion();
30
+
31
+protected:
32
+    std::shared_ptr<LibNfc> _libNfc;
33
+};
34
+
35
+
36
+#endif //MIFARE_TOOLS_LIBNFCBUSINESS_H

+ 40
- 0
src/Business/NfcDeviceBusiness.cpp View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#include "NfcDeviceBusiness.h"
6
+
7
+NfcDeviceBusiness::NfcDeviceBusiness(std::shared_ptr<NfcDevice> device)
8
+    : _device(device)
9
+{
10
+}
11
+
12
+ResultBool NfcDeviceBusiness::open()
13
+{
14
+    if (isOpened()) {
15
+        return ResultBool::error("NFC device is already opened");
16
+    }
17
+    return _device->open();
18
+}
19
+
20
+void NfcDeviceBusiness::close()
21
+{
22
+    if (isOpened()) {
23
+        _device->close();
24
+    }
25
+}
26
+
27
+bool NfcDeviceBusiness::isOpened() const
28
+{
29
+    return _device->getDevice() != 0;
30
+}
31
+
32
+const std::string &NfcDeviceBusiness::getConnStr() const
33
+{
34
+    return _device->getConnStr();
35
+}
36
+
37
+std::shared_ptr<NfcDevice> NfcDeviceBusiness::getDevice() const
38
+{
39
+    return _device;
40
+}

+ 31
- 0
src/Business/NfcDeviceBusiness.h View File

1
+//
2
+// Created by robin on 7/22/16.
3
+//
4
+
5
+#ifndef MIFARE_TOOLS_NFCDEVICEBUSINESS_H
6
+#define MIFARE_TOOLS_NFCDEVICEBUSINESS_H
7
+
8
+
9
+#include <DataAccess/NfcDevice.h>
10
+
11
+class NfcDeviceBusiness
12
+{
13
+public:
14
+    NfcDeviceBusiness(std::shared_ptr<NfcDevice> device);
15
+
16
+    ResultBool open();
17
+
18
+    void close();
19
+
20
+    bool isOpened() const;
21
+
22
+    const std::string & getConnStr() const;
23
+
24
+    std::shared_ptr<NfcDevice> getDevice() const;
25
+
26
+protected:
27
+    std::shared_ptr<NfcDevice> _device;
28
+};
29
+
30
+
31
+#endif //MIFARE_TOOLS_NFCDEVICEBUSINESS_H

+ 9
- 9
src/CMakeLists.txt View File

3
 set(SOURCE_FILES
3
 set(SOURCE_FILES
4
         DBO/Result.hxx
4
         DBO/Result.hxx
5
         DBO/Result.h
5
         DBO/Result.h
6
-        Business/LibNfc.cpp
7
-        Business/LibNfc.h
8
-        Business/NfcDevice.cpp
9
-        Business/NfcDevice.h
10
-        Business/FreeFareDevice.cpp
11
-        Business/FreeFareDevice.h
12
-        Business/FreeFareTag.cpp
13
-        Business/FreeFareTag.h
14
-        Business/StringUtils.cpp Business/StringUtils.h DBO/AccessBitsDbo.cpp DBO/AccessBitsDbo.h)
6
+        DataAccess/LibNfc.cpp
7
+        DataAccess/LibNfc.h
8
+        DataAccess/NfcDevice.cpp
9
+        DataAccess/NfcDevice.h
10
+        DataAccess/FreeFareDevice.cpp
11
+        DataAccess/FreeFareDevice.h
12
+        DataAccess/FreeFareTag.cpp
13
+        DataAccess/FreeFareTag.h
14
+        DBO/StringUtils.cpp DBO/StringUtils.h DBO/AccessBitsDbo.cpp DBO/AccessBitsDbo.h Business/LibNfcBusiness.cpp Business/LibNfcBusiness.h Business/NfcDeviceBusiness.cpp Business/NfcDeviceBusiness.h Business/FreeFareDeviceBusiness.cpp Business/FreeFareDeviceBusiness.h Business/FreeFareTagBusiness.cpp Business/FreeFareTagBusiness.h)
15
 
15
 
16
 add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
16
 add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
17
 target_link_libraries(${PROJECT_NAME} ${LIBS})
17
 target_link_libraries(${PROJECT_NAME} ${LIBS})

+ 24
- 24
src/DBO/AccessBitsDbo.cpp View File

28
 {
28
 {
29
 }
29
 }
30
 
30
 
31
-const char AccessBitsDbo::getUserData() const
31
+char AccessBitsDbo::getUserData() const
32
 {
32
 {
33
     return _bits[_bits.length() - 1];
33
     return _bits[_bits.length() - 1];
34
 }
34
 }
68
     setArrayBit((char*)buf, bitPosition, value);
68
     setArrayBit((char*)buf, bitPosition, value);
69
 }
69
 }
70
 
70
 
71
-const bool AccessBitsDbo::getBit(const char &i, const char &j) const
71
+bool AccessBitsDbo::getBit(const char &i, const char &j) const
72
 {
72
 {
73
     const char* buf = _bits.c_str();
73
     const char* buf = _bits.c_str();
74
     return getArrayBit(buf, nonInvertedBitPosition[i][j]) && !getArrayBit(buf, invertedBitPosition[i][j]);
74
     return getArrayBit(buf, nonInvertedBitPosition[i][j]) && !getArrayBit(buf, invertedBitPosition[i][j]);
75
 }
75
 }
76
 
76
 
77
-const bool AccessBitsDbo::getArrayBit(const char *buf, const char &bitPosition)
77
+bool AccessBitsDbo::getArrayBit(const char *buf, const char &bitPosition)
78
 {
78
 {
79
     char byteOffset = (char)(bitPosition / 8);
79
     char byteOffset = (char)(bitPosition / 8);
80
     char bitOffset = (char)(7 - (bitPosition % 8));
80
     char bitOffset = (char)(7 - (bitPosition % 8));
81
     return (buf[byteOffset] >> bitOffset) & 1 == 1;
81
     return (buf[byteOffset] >> bitOffset) & 1 == 1;
82
 }
82
 }
83
 
83
 
84
-const bool AccessBitsDbo::getArrayBit(const unsigned char *buf, const char &bitPosition)
84
+bool AccessBitsDbo::getArrayBit(const unsigned char *buf, const char &bitPosition)
85
 {
85
 {
86
     return getArrayBit((const char *)buf, bitPosition);
86
     return getArrayBit((const char *)buf, bitPosition);
87
 }
87
 }
88
 
88
 
89
-const bool AccessBitsDbo::canKeyAReadBlock(const char &block) const
89
+bool AccessBitsDbo::canKeyAReadBlock(const char &block) const
90
 {
90
 {
91
     bool c1 = getBit(1, block);
91
     bool c1 = getBit(1, block);
92
     bool c2 = getBit(2, block);
92
     bool c2 = getBit(2, block);
94
     return !c3 || (!c1 && !c2 && c3);
94
     return !c3 || (!c1 && !c2 && c3);
95
 }
95
 }
96
 
96
 
97
-const bool AccessBitsDbo::canKeyBReadBlock(const char &block) const
97
+bool AccessBitsDbo::canKeyBReadBlock(const char &block) const
98
 {
98
 {
99
     bool c1 = getBit(1, block);
99
     bool c1 = getBit(1, block);
100
     bool c2 = getBit(2, block);
100
     bool c2 = getBit(2, block);
102
     return !c1 || !c2 || !c3;
102
     return !c1 || !c2 || !c3;
103
 }
103
 }
104
 
104
 
105
-const bool AccessBitsDbo::canKeyAWriteBlock(const char &block) const
105
+bool AccessBitsDbo::canKeyAWriteBlock(const char &block) const
106
 {
106
 {
107
     bool c1 = getBit(1, block);
107
     bool c1 = getBit(1, block);
108
     bool c2 = getBit(2, block);
108
     bool c2 = getBit(2, block);
110
     return !c1 && !c2 && !c3;
110
     return !c1 && !c2 && !c3;
111
 }
111
 }
112
 
112
 
113
-const bool AccessBitsDbo::canKeyBWriteBlock(const char &block) const
113
+bool AccessBitsDbo::canKeyBWriteBlock(const char &block) const
114
 {
114
 {
115
     bool c1 = getBit(1, block);
115
     bool c1 = getBit(1, block);
116
     bool c2 = getBit(2, block);
116
     bool c2 = getBit(2, block);
118
     return (!c2 && !c3) || (c1 && !c3) || (!c1 && c2 && c3);
118
     return (!c2 && !c3) || (c1 && !c3) || (!c1 && c2 && c3);
119
 }
119
 }
120
 
120
 
121
-const bool AccessBitsDbo::canKeyAIncrementBlock(const char &block) const
121
+bool AccessBitsDbo::canKeyAIncrementBlock(const char &block) const
122
 {
122
 {
123
     bool c1 = getBit(1, block);
123
     bool c1 = getBit(1, block);
124
     bool c2 = getBit(2, block);
124
     bool c2 = getBit(2, block);
126
     return !c1 && !c2 && !c3;
126
     return !c1 && !c2 && !c3;
127
 }
127
 }
128
 
128
 
129
-const bool AccessBitsDbo::canKeyBIncrementBlock(const char &block) const
129
+bool AccessBitsDbo::canKeyBIncrementBlock(const char &block) const
130
 {
130
 {
131
     bool c1 = getBit(1, block);
131
     bool c1 = getBit(1, block);
132
     bool c2 = getBit(2, block);
132
     bool c2 = getBit(2, block);
134
     return (!c1 && !c2 && !c3) || (c1 && c2 && !c3);
134
     return (!c1 && !c2 && !c3) || (c1 && c2 && !c3);
135
 }
135
 }
136
 
136
 
137
-const bool AccessBitsDbo::canKeyADecrementBlock(const char &block) const
137
+bool AccessBitsDbo::canKeyADecrementBlock(const char &block) const
138
 {
138
 {
139
     bool c1 = getBit(1, block);
139
     bool c1 = getBit(1, block);
140
     bool c2 = getBit(2, block);
140
     bool c2 = getBit(2, block);
142
     return (!c1 && !c2) || (c1 && c2 && !c3);
142
     return (!c1 && !c2) || (c1 && c2 && !c3);
143
 }
143
 }
144
 
144
 
145
-const bool AccessBitsDbo::canKeyBDecrementBlock(const char &block) const
145
+bool AccessBitsDbo::canKeyBDecrementBlock(const char &block) const
146
 {
146
 {
147
     bool c1 = getBit(1, block);
147
     bool c1 = getBit(1, block);
148
     bool c2 = getBit(2, block);
148
     bool c2 = getBit(2, block);
150
     return (!c1 && !c2) || (c1 && c2 && !c3);
150
     return (!c1 && !c2) || (c1 && c2 && !c3);
151
 }
151
 }
152
 
152
 
153
-const bool AccessBitsDbo::canKeyAReadKeyATrailer() const
153
+bool AccessBitsDbo::canKeyAReadKeyATrailer() const
154
 {
154
 {
155
     return false;
155
     return false;
156
 }
156
 }
157
 
157
 
158
-const bool AccessBitsDbo::canKeyBReadKeyATrailer() const
158
+bool AccessBitsDbo::canKeyBReadKeyATrailer() const
159
 {
159
 {
160
     return false;
160
     return false;
161
 }
161
 }
162
 
162
 
163
-const bool AccessBitsDbo::canKeyAWriteKeyATrailer() const
163
+bool AccessBitsDbo::canKeyAWriteKeyATrailer() const
164
 {
164
 {
165
     bool c1 = getBit(1, 3);
165
     bool c1 = getBit(1, 3);
166
     bool c2 = getBit(2, 3);
166
     bool c2 = getBit(2, 3);
168
     return !c1 && !c2;
168
     return !c1 && !c2;
169
 }
169
 }
170
 
170
 
171
-const bool AccessBitsDbo::canKeyBWriteKeyATrailer() const
171
+bool AccessBitsDbo::canKeyBWriteKeyATrailer() const
172
 {
172
 {
173
     bool c1 = getBit(1, 3);
173
     bool c1 = getBit(1, 3);
174
     bool c2 = getBit(2, 3);
174
     bool c2 = getBit(2, 3);
176
     return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
176
     return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
177
 }
177
 }
178
 
178
 
179
-const bool AccessBitsDbo::canKeyAReadAccessBitsTrailer() const
179
+bool AccessBitsDbo::canKeyAReadAccessBitsTrailer() const
180
 {
180
 {
181
     return true;
181
     return true;
182
 }
182
 }
183
 
183
 
184
-const bool AccessBitsDbo::canKeyBReadAccessBitsTrailer() const
184
+bool AccessBitsDbo::canKeyBReadAccessBitsTrailer() const
185
 {
185
 {
186
     bool c1 = getBit(1, 3);
186
     bool c1 = getBit(1, 3);
187
     bool c2 = getBit(2, 3);
187
     bool c2 = getBit(2, 3);
189
     return c1 || (c2 && c3);
189
     return c1 || (c2 && c3);
190
 }
190
 }
191
 
191
 
192
-const bool AccessBitsDbo::canKeyAWriteAccessBitsTrailer() const
192
+bool AccessBitsDbo::canKeyAWriteAccessBitsTrailer() const
193
 {
193
 {
194
     bool c1 = getBit(1, 3);
194
     bool c1 = getBit(1, 3);
195
     bool c2 = getBit(2, 3);
195
     bool c2 = getBit(2, 3);
197
     return !c1 && !c2 && c3;
197
     return !c1 && !c2 && c3;
198
 }
198
 }
199
 
199
 
200
-const bool AccessBitsDbo::canKeyBWriteAccessBitsTrailer() const
200
+bool AccessBitsDbo::canKeyBWriteAccessBitsTrailer() const
201
 {
201
 {
202
     bool c1 = getBit(1, 3);
202
     bool c1 = getBit(1, 3);
203
     bool c2 = getBit(2, 3);
203
     bool c2 = getBit(2, 3);
205
     return (!c1 && c2 && c3) || (c1 && !c2 && c3);
205
     return (!c1 && c2 && c3) || (c1 && !c2 && c3);
206
 }
206
 }
207
 
207
 
208
-const bool AccessBitsDbo::canKeyAReadKeyBTrailer() const
208
+bool AccessBitsDbo::canKeyAReadKeyBTrailer() const
209
 {
209
 {
210
     bool c1 = getBit(1, 3);
210
     bool c1 = getBit(1, 3);
211
     bool c2 = getBit(2, 3);
211
     bool c2 = getBit(2, 3);
213
     return (!c1 && !c2) || (!c1 && !c3);
213
     return (!c1 && !c2) || (!c1 && !c3);
214
 }
214
 }
215
 
215
 
216
-const bool AccessBitsDbo::canKeyBReadKeyBTrailer() const
216
+bool AccessBitsDbo::canKeyBReadKeyBTrailer() const
217
 {
217
 {
218
     return false;
218
     return false;
219
 }
219
 }
220
 
220
 
221
-const bool AccessBitsDbo::canKeyAWriteKeyBTrailer() const
221
+bool AccessBitsDbo::canKeyAWriteKeyBTrailer() const
222
 {
222
 {
223
     bool c1 = getBit(1, 3);
223
     bool c1 = getBit(1, 3);
224
     bool c2 = getBit(2, 3);
224
     bool c2 = getBit(2, 3);
226
     return !c1 && !c2;
226
     return !c1 && !c2;
227
 }
227
 }
228
 
228
 
229
-const bool AccessBitsDbo::canKeyBWriteKeyBTrailer() const
229
+bool AccessBitsDbo::canKeyBWriteKeyBTrailer() const
230
 {
230
 {
231
     bool c1 = getBit(1, 3);
231
     bool c1 = getBit(1, 3);
232
     bool c2 = getBit(2, 3);
232
     bool c2 = getBit(2, 3);

+ 24
- 24
src/DBO/AccessBitsDbo.h View File

22
     AccessBitsDbo();
22
     AccessBitsDbo();
23
     AccessBitsDbo(const std::string& bits);
23
     AccessBitsDbo(const std::string& bits);
24
 
24
 
25
-    const char getUserData() const;
25
+    char getUserData() const;
26
     void setUserData(const char& data);
26
     void setUserData(const char& data);
27
 
27
 
28
     void setBit(const char& i, const char& j, const bool& value);
28
     void setBit(const char& i, const char& j, const bool& value);
29
-    const bool getBit(const char& i, const char& j) const;
29
+    bool getBit(const char& i, const char& j) const;
30
 
30
 
31
-    const bool canKeyAReadBlock(const char& block) const;
32
-    const bool canKeyBReadBlock(const char& block) const;
31
+    bool canKeyAReadBlock(const char& block) const;
32
+    bool canKeyBReadBlock(const char& block) const;
33
 
33
 
34
-    const bool canKeyAWriteBlock(const char& block) const;
35
-    const bool canKeyBWriteBlock(const char& block) const;
34
+    bool canKeyAWriteBlock(const char& block) const;
35
+    bool canKeyBWriteBlock(const char& block) const;
36
 
36
 
37
-    const bool canKeyAIncrementBlock(const char& block) const;
38
-    const bool canKeyBIncrementBlock(const char& block) const;
37
+    bool canKeyAIncrementBlock(const char& block) const;
38
+    bool canKeyBIncrementBlock(const char& block) const;
39
 
39
 
40
-    const bool canKeyADecrementBlock(const char& block) const;
41
-    const bool canKeyBDecrementBlock(const char& block) const;
40
+    bool canKeyADecrementBlock(const char& block) const;
41
+    bool canKeyBDecrementBlock(const char& block) const;
42
 
42
 
43
-    const bool canKeyAReadKeyATrailer() const;
44
-    const bool canKeyBReadKeyATrailer() const;
43
+    bool canKeyAReadKeyATrailer() const;
44
+    bool canKeyBReadKeyATrailer() const;
45
 
45
 
46
-    const bool canKeyAWriteKeyATrailer() const;
47
-    const bool canKeyBWriteKeyATrailer() const;
46
+    bool canKeyAWriteKeyATrailer() const;
47
+    bool canKeyBWriteKeyATrailer() const;
48
 
48
 
49
-    const bool canKeyAReadAccessBitsTrailer() const;
50
-    const bool canKeyBReadAccessBitsTrailer() const;
49
+    bool canKeyAReadAccessBitsTrailer() const;
50
+    bool canKeyBReadAccessBitsTrailer() const;
51
 
51
 
52
-    const bool canKeyAWriteAccessBitsTrailer() const;
53
-    const bool canKeyBWriteAccessBitsTrailer() const;
52
+    bool canKeyAWriteAccessBitsTrailer() const;
53
+    bool canKeyBWriteAccessBitsTrailer() const;
54
 
54
 
55
-    const bool canKeyAReadKeyBTrailer() const;
56
-    const bool canKeyBReadKeyBTrailer() const;
55
+    bool canKeyAReadKeyBTrailer() const;
56
+    bool canKeyBReadKeyBTrailer() const;
57
 
57
 
58
-    const bool canKeyAWriteKeyBTrailer() const;
59
-    const bool canKeyBWriteKeyBTrailer() const;
58
+    bool canKeyAWriteKeyBTrailer() const;
59
+    bool canKeyBWriteKeyBTrailer() const;
60
 
60
 
61
     static void setArrayBit(char *buf, const char &bitPosition, const bool &value);
61
     static void setArrayBit(char *buf, const char &bitPosition, const bool &value);
62
     static void setArrayBit(unsigned char *buf, const char &bitPosition, const bool &value);
62
     static void setArrayBit(unsigned char *buf, const char &bitPosition, const bool &value);
63
-    static const bool getArrayBit(const char *buf, const char &bitPosition);
64
-    static const bool getArrayBit(const unsigned char *buf, const char &bitPosition);
63
+    static bool getArrayBit(const char *buf, const char &bitPosition);
64
+    static bool getArrayBit(const unsigned char *buf, const char &bitPosition);
65
 
65
 
66
 private:
66
 private:
67
     std::string _bits;
67
     std::string _bits;

src/Business/StringUtils.cpp → src/DBO/StringUtils.cpp View File


src/Business/StringUtils.h → src/DBO/StringUtils.h View File


src/Business/FreeFareDevice.cpp → src/DataAccess/FreeFareDevice.cpp View File

11
 
11
 
12
 Result<std::vector<std::shared_ptr<FreeFareTag>>> FreeFareDevice::getTags()
12
 Result<std::vector<std::shared_ptr<FreeFareTag>>> FreeFareDevice::getTags()
13
 {
13
 {
14
-    if (!_device->isOpened()) {
15
-        return Result<std::vector<std::shared_ptr<FreeFareTag>>>::error("Device is not opened");
16
-    }
17
     FreefareTag* tags = freefare_get_tags(_device->getDevice());
14
     FreefareTag* tags = freefare_get_tags(_device->getDevice());
18
     if (!tags) {
15
     if (!tags) {
19
         return Result<std::vector<std::shared_ptr<FreeFareTag>>>::error("Failed to get MIFARE tags");
16
         return Result<std::vector<std::shared_ptr<FreeFareTag>>>::error("Failed to get MIFARE tags");

src/Business/FreeFareDevice.h → src/DataAccess/FreeFareDevice.h View File


src/Business/FreeFareTag.cpp → src/DataAccess/FreeFareTag.cpp View File

33
     return _uid;
33
     return _uid;
34
 }
34
 }
35
 
35
 
36
-ResultBool FreeFareTag::authenticate(int sector, std::string key, int keyType)
36
+ResultBool FreeFareTag::authenticate(int sector, int block, std::string key, int keyType)
37
 {
37
 {
38
     if (mifare_classic_connect(_tag) != 0) {
38
     if (mifare_classic_connect(_tag) != 0) {
39
         return ResultBool::error("Failed to connect to MIFARE tag");
39
         return ResultBool::error("Failed to connect to MIFARE tag");
40
     }
40
     }
41
 
41
 
42
-    MifareClassicBlockNumber block = mifare_classic_sector_last_block((MifareClassicBlockNumber)sector);
43
-    if (mifare_classic_authenticate(_tag, block, (const unsigned char*)key.c_str(), (MifareClassicKeyType)keyType) != 0) {
42
+//    MifareClassicBlockNumber block = mifare_classic_sector_last_block((MifareClassicBlockNumber)sector);
43
+    if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(), (MifareClassicKeyType)keyType) != 0) {
44
         return ResultBool::error("Failed to authenticate to MIFARE tag");
44
         return ResultBool::error("Failed to authenticate to MIFARE tag");
45
     }
45
     }
46
 
46
 
48
     return ResultBool::ok(true);
48
     return ResultBool::ok(true);
49
 }
49
 }
50
 
50
 
51
-ResultString FreeFareTag::readSector(int sector, std::string key, int keyType)
52
-{
53
-    if (mifare_classic_connect(_tag) != 0) {
54
-        return ResultString::error("Failed to connect to MIFARE tag");
55
-    }
56
-
57
-    MifareClassicBlockNumber firstBlock = mifare_classic_sector_first_block((MifareClassicBlockNumber)sector);
58
-    MifareClassicBlockNumber lastBlock = mifare_classic_sector_last_block((MifareClassicBlockNumber)sector);
59
-    std::string allData;
60
-    for (MifareClassicBlockNumber block = firstBlock; block <= lastBlock; ++block) {
61
-        if (mifare_classic_authenticate(_tag, block, (const unsigned char *) key.c_str(),
62
-                                        (MifareClassicKeyType) keyType) != 0) {
63
-            return ResultString::error("Failed to authenticate to MIFARE tag");
64
-        }
65
-
66
-        MifareClassicBlock data;
67
-        if (mifare_classic_read(_tag, block, &data)) {
68
-            return ResultString::error("Failed to read MIFARE tag data");
69
-        }
70
-        allData += std::string((const char*)data, sizeof(data));
71
-    }
72
-
73
-    mifare_classic_disconnect(_tag);
74
-    return ResultString::ok(allData);
75
-}
76
-
77
 ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
51
 ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
78
 {
52
 {
79
     if (mifare_classic_connect(_tag) != 0) {
53
     if (mifare_classic_connect(_tag) != 0) {

src/Business/FreeFareTag.h → src/DataAccess/FreeFareTag.h View File

16
     FreeFareTag(FreefareTag tag);
16
     FreeFareTag(FreefareTag tag);
17
     ~FreeFareTag();
17
     ~FreeFareTag();
18
 
18
 
19
-    ResultBool authenticate(int sector, std::string key, int keyType);
20
-
21
-    ResultString readSector(int sector, std::string key, int keyType);
19
+    ResultBool authenticate(int sector, int block, std::string key, int keyType);
22
 
20
 
23
     ResultString readBlock(int sector, int block, std::string key, int keyType);
21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
24
 
22
 

src/Business/LibNfc.cpp → src/DataAccess/LibNfc.cpp View File

10
 {
10
 {
11
 }
11
 }
12
 
12
 
13
+LibNfc::~LibNfc()
14
+{
15
+    clean();
16
+}
17
+
13
 ResultBool LibNfc::init()
18
 ResultBool LibNfc::init()
14
 {
19
 {
15
-    if (isInitialized()) {
16
-        return ResultBool::error("LibNfc is already initialized");
17
-    }
18
     nfc_init(&_context);
20
     nfc_init(&_context);
19
     if (!_context) {
21
     if (!_context) {
20
         return ResultBool::error("LibNfc could not be initialized");
22
         return ResultBool::error("LibNfc could not be initialized");
27
     return nfc_version();
29
     return nfc_version();
28
 }
30
 }
29
 
31
 
30
-LibNfc::~LibNfc()
31
-{
32
-    clean();
33
-}
34
-
35
-bool LibNfc::isInitialized()
36
-{
37
-    return _context != 0;
38
-}
39
-
40
 void LibNfc::clean()
32
 void LibNfc::clean()
41
 {
33
 {
42
-    if (isInitialized()) {
43
-        nfc_exit(_context);
44
-        _context = 0;
45
-    }
34
+    nfc_exit(_context);
35
+    _context = 0;
46
 }
36
 }
47
 
37
 
48
-Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfc::getDevices()
38
+Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfc::getDevices() const
49
 {
39
 {
50
-    if (!isInitialized()) {
51
-        return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("LibNfc is not initialized");
52
-    }
53
     nfc_connstring devices[16];
40
     nfc_connstring devices[16];
54
     size_t count = nfc_list_devices(_context, devices, sizeof(devices));
41
     size_t count = nfc_list_devices(_context, devices, sizeof(devices));
55
     if (count < 0) {
42
     if (count < 0) {
56
-        return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("Failed to list NFC devices");
43
+        return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("Failed to list NFC devices: " + count);
57
     }
44
     }
58
     std::vector<std::shared_ptr<NfcDevice>> devicesList;
45
     std::vector<std::shared_ptr<NfcDevice>> devicesList;
59
     for (size_t i = 0; i < count; ++i) {
46
     for (size_t i = 0; i < count; ++i) {

src/Business/LibNfc.h → src/DataAccess/LibNfc.h View File

16
 {
16
 {
17
 public:
17
 public:
18
     LibNfc();
18
     LibNfc();
19
-    ~LibNfc();
19
+    virtual ~LibNfc();
20
 
20
 
21
     ResultBool init();
21
     ResultBool init();
22
 
22
 
23
     void clean();
23
     void clean();
24
 
24
 
25
-    bool isInitialized();
26
-
27
-    Result<std::vector<std::shared_ptr<NfcDevice>>> getDevices();
25
+    Result<std::vector<std::shared_ptr<NfcDevice>>> getDevices() const;
28
 
26
 
29
     static std::string getVersion();
27
     static std::string getVersion();
30
 
28
 
31
     nfc_context* getContext() const;
29
     nfc_context* getContext() const;
32
 
30
 
33
-private:
31
+protected:
34
     nfc_context* _context;
32
     nfc_context* _context;
35
 };
33
 };
36
 
34
 

src/Business/NfcDevice.cpp → src/DataAccess/NfcDevice.cpp View File

4
 
4
 
5
 #include "NfcDevice.h"
5
 #include "NfcDevice.h"
6
 
6
 
7
-NfcDevice::NfcDevice(LibNfc* libNfc, std::string str)
7
+NfcDevice::NfcDevice(const LibNfc* libNfc, const std::string& str)
8
     : _connStr(str)
8
     : _connStr(str)
9
     , _device(0)
9
     , _device(0)
10
     , _libNfc(libNfc)
10
     , _libNfc(libNfc)
18
 
18
 
19
 ResultBool NfcDevice::open()
19
 ResultBool NfcDevice::open()
20
 {
20
 {
21
-    if (isOpened()) {
22
-        return ResultBool::error("NFC device is already opened");
23
-    }
24
     _device = nfc_open(_libNfc->getContext(), _connStr.c_str());
21
     _device = nfc_open(_libNfc->getContext(), _connStr.c_str());
25
     if (!_device) {
22
     if (!_device) {
26
         return ResultBool::error("Failed to open NFC device");
23
         return ResultBool::error("Failed to open NFC device");
28
     return ResultBool::ok(true);
25
     return ResultBool::ok(true);
29
 }
26
 }
30
 
27
 
31
-bool NfcDevice::isOpened()
32
-{
33
-    return _device != 0;
34
-}
35
-
36
 void NfcDevice::close()
28
 void NfcDevice::close()
37
 {
29
 {
38
-    if (isOpened()) {
39
-        nfc_close(_device);
40
-        _device = 0;
41
-    }
30
+    nfc_close(_device);
31
+    _device = 0;
42
 }
32
 }
43
 
33
 
44
 nfc_device *NfcDevice::getDevice() const
34
 nfc_device *NfcDevice::getDevice() const
46
     return _device;
36
     return _device;
47
 }
37
 }
48
 
38
 
49
-void NfcDevice::setConnStr(const std::string &connStr)
50
-{
51
-    _connStr = connStr;
52
-}
53
-
54
 const std::string &NfcDevice::getConnStr() const
39
 const std::string &NfcDevice::getConnStr() const
55
 {
40
 {
56
     return _connStr;
41
     return _connStr;

src/Business/NfcDevice.h → src/DataAccess/NfcDevice.h View File

16
 class NfcDevice
16
 class NfcDevice
17
 {
17
 {
18
 public:
18
 public:
19
-    NfcDevice(LibNfc* libNfc, std::string connStr);
19
+    NfcDevice(const LibNfc* libNfc, const std::string& connStr);
20
     ~NfcDevice();
20
     ~NfcDevice();
21
 
21
 
22
     ResultBool open();
22
     ResultBool open();
23
 
23
 
24
     void close();
24
     void close();
25
 
25
 
26
-    bool isOpened();
27
-
28
     const std::string & getConnStr() const;
26
     const std::string & getConnStr() const;
29
 
27
 
30
-    void setConnStr(const std::string &connStr);
31
-
32
     nfc_device * getDevice() const;
28
     nfc_device * getDevice() const;
33
 
29
 
34
 private:
30
 private:
36
 
32
 
37
     nfc_device* _device;
33
     nfc_device* _device;
38
 
34
 
39
-    LibNfc* _libNfc;
35
+    const LibNfc* _libNfc;
40
 };
36
 };
41
 
37
 
42
 
38
 

+ 2
- 2
tests/test-main.cpp View File

1
 #include <iostream>
1
 #include <iostream>
2
 #include <string.h>
2
 #include <string.h>
3
 #include <gtest/gtest.h>
3
 #include <gtest/gtest.h>
4
-#include <Business/LibNfc.h>
5
-#include <Business/StringUtils.h>
4
+#include <DataAccess/LibNfc.h>
5
+#include <DBO/StringUtils.h>
6
 #include <DBO/AccessBitsDbo.h>
6
 #include <DBO/AccessBitsDbo.h>
7
 
7
 
8
 TEST(StringUtils, rawToHumanChar)
8
 TEST(StringUtils, rawToHumanChar)

Loading…
Cancel
Save