Ver código fonte

use shared pointer; begin freefaretag

develop
Robin Thoni 8 anos atrás
pai
commit
e41ffc15b5

+ 1
- 1
CMakeLists.txt Ver arquivo

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

+ 1
- 1
src/CMakeLists.txt Ver arquivo

@@ -13,7 +13,7 @@ set(SOURCE_FILES
13 13
   DataAccess/LibNfc.cpp
14 14
   DataAccess/LibNfc.h
15 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 18
 add_executable(${PROJECT_NAME} ${SOURCE_FILES})
19 19
 target_link_libraries(${PROJECT_NAME} ${LIBS})

+ 0
- 0
src/DataAccess/.gitkeep Ver arquivo


+ 17
- 1
src/DataAccess/FreeFareDevice.cpp Ver arquivo

@@ -4,7 +4,23 @@
4 4
 
5 5
 #include "FreeFareDevice.h"
6 6
 
7
-FreeFareDevice::FreeFareDevice(NfcDevice *device)
7
+FreeFareDevice::FreeFareDevice(std::shared_ptr<NfcDevice> device)
8 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 Ver arquivo

@@ -6,16 +6,19 @@
6 6
 #define MIFARE_TOOLS_FREEFAREDEVICE_H
7 7
 
8 8
 
9
-#include "NfcDevice.h"
10 9
 #include <freefare.h>
10
+#include "NfcDevice.h"
11
+#include "FreeFareTag.h"
11 12
 
12 13
 class FreeFareDevice
13 14
 {
14 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 20
 private:
18
-    NfcDevice* _device;
21
+    std::shared_ptr<NfcDevice> _device;
19 22
 };
20 23
 
21 24
 

+ 32
- 0
src/DataAccess/FreeFareTag.cpp Ver arquivo

@@ -0,0 +1,32 @@
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 Ver arquivo

@@ -0,0 +1,32 @@
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 Ver arquivo

@@ -2,6 +2,7 @@
2 2
 // Created by robin on 6/19/16.
3 3
 //
4 4
 
5
+#include <boost/shared_ptr.hpp>
5 6
 #include "LibNfc.h"
6 7
 
7 8
 LibNfc::LibNfc()
@@ -44,21 +45,21 @@ void LibNfc::clean()
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 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 53
     nfc_connstring devices[16];
53 54
     size_t count = nfc_list_devices(_context, devices, sizeof(devices));
54 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 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 65
 nfc_context *LibNfc::getContext() const

+ 2
- 1
src/DataAccess/LibNfc.h Ver arquivo

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

+ 15
- 0
src/DataAccess/NfcDevice.cpp Ver arquivo

@@ -40,3 +40,18 @@ void NfcDevice::close()
40 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 Ver arquivo

@@ -25,15 +25,11 @@ public:
25 25
 
26 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 34
 private:
39 35
     std::string _connStr;

+ 15
- 3
src/Interface/MainClass.cpp Ver arquivo

@@ -6,6 +6,7 @@
6 6
 #include <sysexits.h>
7 7
 #include <DBO/Result.h>
8 8
 #include <DataAccess/LibNfc.h>
9
+#include <DataAccess/FreeFareDevice.h>
9 10
 #include "CommandLineParser.h"
10 11
 #include "MainClass.h"
11 12
 
@@ -40,9 +41,6 @@ int MainClass::main()
40 41
     for (size_t i = 0; i < devices.getData().size(); ++i) {
41 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 45
     auto device = devices.getData()[0];
48 46
     auto open = device->open();
@@ -51,9 +49,23 @@ int MainClass::main()
51 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 66
     device->close();
57 67
 
68
+    libNfc.clean();
69
+
58 70
     return 0;
59 71
 }

Carregando…
Cancelar
Salvar