Ver código fonte

fixed block read

develop
Robin Thoni 7 anos atrás
pai
commit
1e9a6d78a2

+ 12
- 5
cli/Interface/MainClass.cpp Ver arquivo

@@ -63,12 +63,19 @@ int MainClass::main()
63 63
     for (size_t i = 0; i < tags.getData().size(); ++i) {
64 64
         auto tag = tags.getData()[i];
65 65
         std::cout << "UID: " << tag->getUid() << std::endl;
66
-        auto sector = tag->readSector(1, StringUtils::humanToRaw("8829da9daf76").getData(), MFC_KEY_A);
67
-        if (!sector) {
68
-            sector.print();
69
-            return 6;
66
+        std::cout << "Type: " << tag->getType() << std::endl;
67
+        for(int i = 0; i < 16; ++i) {
68
+            std::cout << "+Sector: " << i << std::endl;
69
+            for (int j = 0; j < 4; ++j) {
70
+                auto sector = tag->readBlock(i, j, StringUtils::humanToRaw("8829da9daf76").getData(), MFC_KEY_A);
71
+                if (!sector) {
72
+                    sector.print();
73
+                }
74
+                else {
75
+                    std::cout << StringUtils::rawToHuman(sector.getData()) << std::endl;
76
+                }
77
+            }
70 78
         }
71
-        std::cout << "Sector 1: " << StringUtils::rawToHuman(sector.getData()) << std::endl;
72 79
     }
73 80
 
74 81
     device->close();

+ 8
- 2
src/Business/FreeFareTagBusiness.cpp Ver arquivo

@@ -22,7 +22,8 @@ ResultString FreeFareTagBusiness::readBlock(int sector, int block, std::string k
22 22
 ResultString FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
23 23
 {
24 24
     std::string res;
25
-    for (int i = 0; i < 4; ++i) {
25
+    int lastBlock = _tag->getSectorBlockCount(sector);
26
+    for (int i = 0; i < lastBlock; ++i) {
26 27
         auto data = readBlock(sector, i, key, keyType);
27 28
         if (data) {
28 29
             res += data.getData();
@@ -39,7 +40,12 @@ const std::string &FreeFareTagBusiness::getUid() const
39 40
     return _tag->getUid();
40 41
 }
41 42
 
43
+freefare_tag_type FreeFareTagBusiness::getType() const
44
+{
45
+    return _tag->getType();
46
+}
47
+
42 48
 std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
43 49
 {
44 50
     return _tag;
45
-}
51
+}

+ 2
- 0
src/Business/FreeFareTagBusiness.h Ver arquivo

@@ -22,6 +22,8 @@ public:
22 22
 
23 23
     const std::string & getUid() const;
24 24
 
25
+    freefare_tag_type getType() const;
26
+
25 27
     std::shared_ptr<FreeFareTag> getTag() const;
26 28
 
27 29
 protected:

+ 31
- 16
src/DataAccess/FreeFareTag.cpp Ver arquivo

@@ -18,21 +18,6 @@ FreeFareTag::~FreeFareTag()
18 18
     freefare_free_tag(_tag);
19 19
 }
20 20
 
21
-const freefare_tag *FreeFareTag::getTag() const
22
-{
23
-    return _tag;
24
-}
25
-
26
-freefare_tag_type FreeFareTag::getType() const
27
-{
28
-    return _type;
29
-}
30
-
31
-const std::string &FreeFareTag::getUid() const
32
-{
33
-    return _uid;
34
-}
35
-
36 21
 ResultBool FreeFareTag::authenticate(int sector, int block, std::string key, int keyType)
37 22
 {
38 23
     if (mifare_classic_connect(_tag) != 0) {
@@ -54,7 +39,7 @@ ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int
54 39
         return ResultString::error("Failed to connect to MIFARE tag");
55 40
     }
56 41
 
57
-    block = mifare_classic_sector_last_block((MifareClassicBlockNumber)sector) + block;
42
+    block = mifare_classic_sector_first_block((MifareClassicBlockNumber)sector) + block;
58 43
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
59 44
                                     (MifareClassicKeyType)keyType) != 0) {
60 45
         return ResultString::error("Failed to authenticate to MIFARE tag");
@@ -68,3 +53,33 @@ ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int
68 53
     mifare_classic_disconnect(_tag);
69 54
     return ResultString::ok(std::string((const char*)data, sizeof(data)));
70 55
 }
56
+
57
+const freefare_tag *FreeFareTag::getTag() const
58
+{
59
+    return _tag;
60
+}
61
+
62
+int FreeFareTag::getFirstBlock(int sector) const
63
+{
64
+    return mifare_classic_sector_first_block(sector);
65
+}
66
+
67
+int FreeFareTag::getLastBlock(int sector) const
68
+{
69
+    return mifare_classic_sector_last_block(sector);
70
+}
71
+
72
+int FreeFareTag::getSectorBlockCount(int sector)
73
+{
74
+    return mifare_classic_sector_block_count(sector);
75
+}
76
+
77
+freefare_tag_type FreeFareTag::getType() const
78
+{
79
+    return _type;
80
+}
81
+
82
+const std::string &FreeFareTag::getUid() const
83
+{
84
+    return _uid;
85
+}

+ 6
- 0
src/DataAccess/FreeFareTag.h Ver arquivo

@@ -20,6 +20,12 @@ public:
20 20
 
21 21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
22 22
 
23
+    int getFirstBlock(int sector) const;
24
+
25
+    int getLastBlock(int sector) const;
26
+
27
+    int getSectorBlockCount(int sector);
28
+
23 29
     const freefare_tag * getTag() const;
24 30
 
25 31
     freefare_tag_type getType() const;

Carregando…
Cancelar
Salvar