Browse Source

use shared pointer; begin freefaretag

develop
Robin Thoni 8 years ago
parent
commit
e41ffc15b5

+ 1
- 1
CMakeLists.txt View File

3
 set(PROJECT_NAME mifare-tools)
3
 set(PROJECT_NAME mifare-tools)
4
 project(${PROJECT_NAME})
4
 project(${PROJECT_NAME})
5
 find_package(GTest)
5
 find_package(GTest)
6
-set(LIBS ${LIBS} nfc)
6
+set(LIBS ${LIBS} nfc freefare)
7
 add_subdirectory(src)
7
 add_subdirectory(src)
8
 if (GTEST_FOUND)
8
 if (GTEST_FOUND)
9
     enable_testing()
9
     enable_testing()

+ 1
- 1
src/CMakeLists.txt View File

13
   DataAccess/LibNfc.cpp
13
   DataAccess/LibNfc.cpp
14
   DataAccess/LibNfc.h
14
   DataAccess/LibNfc.h
15
         DataAccess/NfcDevice.cpp
15
         DataAccess/NfcDevice.cpp
16
-        DataAccess/NfcDevice.h DataAccess/FreeFareDevice.cpp DataAccess/FreeFareDevice.h)
16
+        DataAccess/NfcDevice.h DataAccess/FreeFareDevice.cpp DataAccess/FreeFareDevice.h DataAccess/FreeFareTag.cpp DataAccess/FreeFareTag.h)
17
 
17
 
18
 add_executable(${PROJECT_NAME} ${SOURCE_FILES})
18
 add_executable(${PROJECT_NAME} ${SOURCE_FILES})
19
 target_link_libraries(${PROJECT_NAME} ${LIBS})
19
 target_link_libraries(${PROJECT_NAME} ${LIBS})

+ 0
- 0
src/DataAccess/.gitkeep View File


+ 17
- 1
src/DataAccess/FreeFareDevice.cpp View File

4
 
4
 
5
 #include "FreeFareDevice.h"
5
 #include "FreeFareDevice.h"
6
 
6
 
7
-FreeFareDevice::FreeFareDevice(NfcDevice *device)
7
+FreeFareDevice::FreeFareDevice(std::shared_ptr<NfcDevice> device)
8
     : _device(device)
8
     : _device(device)
9
 {
9
 {
10
 }
10
 }
11
+
12
+Result<std::vector<std::shared_ptr<FreeFareTag>>> FreeFareDevice::getTags()
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());
18
+    if (!tags) {
19
+        return Result<std::vector<std::shared_ptr<FreeFareTag>>>::error("Failed to get MIFARE tags");
20
+    }
21
+    std::vector<std::shared_ptr<FreeFareTag>> tagList;
22
+    for (size_t i = 0; tags[i] != 0; ++i) {
23
+        tagList.push_back(std::make_shared<FreeFareTag>(tags[i]));
24
+    }
25
+    return Result<std::vector<std::shared_ptr<FreeFareTag>>>::ok(tagList);
26
+}

+ 6
- 3
src/DataAccess/FreeFareDevice.h View File

6
 #define MIFARE_TOOLS_FREEFAREDEVICE_H
6
 #define MIFARE_TOOLS_FREEFAREDEVICE_H
7
 
7
 
8
 
8
 
9
-#include "NfcDevice.h"
10
 #include <freefare.h>
9
 #include <freefare.h>
10
+#include "NfcDevice.h"
11
+#include "FreeFareTag.h"
11
 
12
 
12
 class FreeFareDevice
13
 class FreeFareDevice
13
 {
14
 {
14
 public:
15
 public:
15
-    FreeFareDevice(NfcDevice* device);
16
+    FreeFareDevice(std::shared_ptr<NfcDevice> device);
17
+
18
+    Result<std::vector<std::shared_ptr<FreeFareTag>>> getTags();
16
 
19
 
17
 private:
20
 private:
18
-    NfcDevice* _device;
21
+    std::shared_ptr<NfcDevice> _device;
19
 };
22
 };
20
 
23
 
21
 
24
 

+ 32
- 0
src/DataAccess/FreeFareTag.cpp View File

1
+//
2
+// Created by robin on 6/19/16.
3
+//
4
+
5
+#include "FreeFareTag.h"
6
+
7
+FreeFareTag::FreeFareTag(FreefareTag tag)
8
+    : _tag(tag)
9
+{
10
+    _type = freefare_get_tag_type(tag);
11
+    _uid = freefare_get_tag_uid(tag);
12
+}
13
+
14
+FreeFareTag::~FreeFareTag()
15
+{
16
+    freefare_free_tag(_tag);
17
+}
18
+
19
+const freefare_tag *FreeFareTag::getTag() const
20
+{
21
+    return _tag;
22
+}
23
+
24
+freefare_tag_type FreeFareTag::getType() const
25
+{
26
+    return _type;
27
+}
28
+
29
+const std::string &FreeFareTag::getUid() const
30
+{
31
+    return _uid;
32
+}

+ 32
- 0
src/DataAccess/FreeFareTag.h View File

1
+//
2
+// Created by robin on 6/19/16.
3
+//
4
+
5
+#ifndef MIFARE_TOOLS_FREEFARETAG_H
6
+#define MIFARE_TOOLS_FREEFARETAG_H
7
+
8
+#include <freefare.h>
9
+#include <string>
10
+
11
+class FreeFareTag
12
+{
13
+public:
14
+    FreeFareTag(FreefareTag tag);
15
+    ~FreeFareTag();
16
+
17
+    const freefare_tag * getTag() const;
18
+
19
+    freefare_tag_type getType() const;
20
+
21
+    const std::string & getUid() const;
22
+
23
+private:
24
+    FreefareTag _tag;
25
+
26
+    freefare_tag_type _type;
27
+
28
+    std::string _uid;
29
+};
30
+
31
+
32
+#endif //MIFARE_TOOLS_FREEFARETAG_H

+ 7
- 6
src/DataAccess/LibNfc.cpp View File

2
 // Created by robin on 6/19/16.
2
 // Created by robin on 6/19/16.
3
 //
3
 //
4
 
4
 
5
+#include <boost/shared_ptr.hpp>
5
 #include "LibNfc.h"
6
 #include "LibNfc.h"
6
 
7
 
7
 LibNfc::LibNfc()
8
 LibNfc::LibNfc()
44
     }
45
     }
45
 }
46
 }
46
 
47
 
47
-Result<std::vector<NfcDevice*>> LibNfc::getDevices()
48
+Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfc::getDevices()
48
 {
49
 {
49
     if (!isInitialized()) {
50
     if (!isInitialized()) {
50
-        return Result<std::vector<NfcDevice*>>::error("LibNfc is not initialized");
51
+        return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("LibNfc is not initialized");
51
     }
52
     }
52
     nfc_connstring devices[16];
53
     nfc_connstring devices[16];
53
     size_t count = nfc_list_devices(_context, devices, sizeof(devices));
54
     size_t count = nfc_list_devices(_context, devices, sizeof(devices));
54
     if (count < 0) {
55
     if (count < 0) {
55
-        return Result<std::vector<NfcDevice*>>::error("Failed to list NFC devices");
56
+        return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("Failed to list NFC devices");
56
     }
57
     }
57
-    std::vector<NfcDevice*> devicesList;
58
+    std::vector<std::shared_ptr<NfcDevice>> devicesList;
58
     for (size_t i = 0; i < count; ++i) {
59
     for (size_t i = 0; i < count; ++i) {
59
-        devicesList.push_back(new NfcDevice(this, devices[i]));
60
+        devicesList.push_back(std::make_shared<NfcDevice>(this, devices[i]));
60
     }
61
     }
61
-    return Result<std::vector<NfcDevice*>>::ok(devicesList);
62
+    return Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesList);
62
 }
63
 }
63
 
64
 
64
 nfc_context *LibNfc::getContext() const
65
 nfc_context *LibNfc::getContext() const

+ 2
- 1
src/DataAccess/LibNfc.h View File

5
 #ifndef MIFARE_TOOLS_LIBNFC_H
5
 #ifndef MIFARE_TOOLS_LIBNFC_H
6
 #define MIFARE_TOOLS_LIBNFC_H
6
 #define MIFARE_TOOLS_LIBNFC_H
7
 
7
 
8
+#include <memory>
8
 #include <nfc/nfc.h>
9
 #include <nfc/nfc.h>
9
 #include "NfcDevice.h"
10
 #include "NfcDevice.h"
10
 #include "DBO/Result.h"
11
 #include "DBO/Result.h"
23
 
24
 
24
     bool isInitialized();
25
     bool isInitialized();
25
 
26
 
26
-    Result<std::vector<NfcDevice*>> getDevices();
27
+    Result<std::vector<std::shared_ptr<NfcDevice>>> getDevices();
27
 
28
 
28
     static std::string getVersion();
29
     static std::string getVersion();
29
 
30
 

+ 15
- 0
src/DataAccess/NfcDevice.cpp View File

40
         _device = 0;
40
         _device = 0;
41
     }
41
     }
42
 }
42
 }
43
+
44
+nfc_device *NfcDevice::getDevice() const
45
+{
46
+    return _device;
47
+}
48
+
49
+void NfcDevice::setConnStr(const std::string &connStr)
50
+{
51
+    _connStr = connStr;
52
+}
53
+
54
+const std::string &NfcDevice::getConnStr() const
55
+{
56
+    return _connStr;
57
+}

+ 5
- 9
src/DataAccess/NfcDevice.h View File

25
 
25
 
26
     bool isOpened();
26
     bool isOpened();
27
 
27
 
28
-    const std::string &getConnStr() const
29
-    {
30
-        return _connStr;
31
-    }
32
-
33
-    void setConnStr(const std::string &connStr)
34
-    {
35
-        _connStr = connStr;
36
-    }
28
+    const std::string & getConnStr() const;
29
+
30
+    void setConnStr(const std::string &connStr);
31
+
32
+    nfc_device * getDevice() const;
37
 
33
 
38
 private:
34
 private:
39
     std::string _connStr;
35
     std::string _connStr;

+ 15
- 3
src/Interface/MainClass.cpp View File

6
 #include <sysexits.h>
6
 #include <sysexits.h>
7
 #include <DBO/Result.h>
7
 #include <DBO/Result.h>
8
 #include <DataAccess/LibNfc.h>
8
 #include <DataAccess/LibNfc.h>
9
+#include <DataAccess/FreeFareDevice.h>
9
 #include "CommandLineParser.h"
10
 #include "CommandLineParser.h"
10
 #include "MainClass.h"
11
 #include "MainClass.h"
11
 
12
 
40
     for (size_t i = 0; i < devices.getData().size(); ++i) {
41
     for (size_t i = 0; i < devices.getData().size(); ++i) {
41
         std::cout << devices.getData()[i]->getConnStr() << std::endl;
42
         std::cout << devices.getData()[i]->getConnStr() << std::endl;
42
     }
43
     }
43
-    for (size_t i = 1; i < devices.getData().size(); ++i) {
44
-        delete devices.getData()[i];
45
-    }
46
 
44
 
47
     auto device = devices.getData()[0];
45
     auto device = devices.getData()[0];
48
     auto open = device->open();
46
     auto open = device->open();
51
         return 4;
49
         return 4;
52
     }
50
     }
53
 
51
 
52
+    FreeFareDevice freeFareDevice(device);
53
+    auto tags = freeFareDevice.getTags();
54
+    if (!tags) {
55
+        tags.print();
56
+        return 5;
57
+    }
54
 
58
 
59
+    std::cout << "Found " << tags.getData().size() << " tags:" << std::endl;
60
+    for (size_t i = 0; i < tags.getData().size(); ++i) {
61
+        auto tag = tags.getData()[i];
62
+        std::cout << "UID: " << tag->getUid() << std::endl;
63
+
64
+    }
55
 
65
 
56
     device->close();
66
     device->close();
57
 
67
 
68
+    libNfc.clean();
69
+
58
     return 0;
70
     return 0;
59
 }
71
 }

Loading…
Cancel
Save