Browse Source

fixed block read

develop
Robin Thoni 9 years ago
parent
commit
1e9a6d78a2

+ 12
- 5
cli/Interface/MainClass.cpp View File

63
     for (size_t i = 0; i < tags.getData().size(); ++i) {
63
     for (size_t i = 0; i < tags.getData().size(); ++i) {
64
         auto tag = tags.getData()[i];
64
         auto tag = tags.getData()[i];
65
         std::cout << "UID: " << tag->getUid() << std::endl;
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
     device->close();
81
     device->close();

+ 8
- 2
src/Business/FreeFareTagBusiness.cpp View File

22
 ResultString FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
22
 ResultString FreeFareTagBusiness::readSector(int sector, std::string key, int keyType)
23
 {
23
 {
24
     std::string res;
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
         auto data = readBlock(sector, i, key, keyType);
27
         auto data = readBlock(sector, i, key, keyType);
27
         if (data) {
28
         if (data) {
28
             res += data.getData();
29
             res += data.getData();
39
     return _tag->getUid();
40
     return _tag->getUid();
40
 }
41
 }
41
 
42
 
43
+freefare_tag_type FreeFareTagBusiness::getType() const
44
+{
45
+    return _tag->getType();
46
+}
47
+
42
 std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
48
 std::shared_ptr<FreeFareTag> FreeFareTagBusiness::getTag() const
43
 {
49
 {
44
     return _tag;
50
     return _tag;
45
-}
51
+}

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

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

+ 31
- 16
src/DataAccess/FreeFareTag.cpp View File

18
     freefare_free_tag(_tag);
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
 ResultBool FreeFareTag::authenticate(int sector, int block, std::string key, int keyType)
21
 ResultBool FreeFareTag::authenticate(int sector, int block, std::string key, int keyType)
37
 {
22
 {
38
     if (mifare_classic_connect(_tag) != 0) {
23
     if (mifare_classic_connect(_tag) != 0) {
54
         return ResultString::error("Failed to connect to MIFARE tag");
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
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
43
     if (mifare_classic_authenticate(_tag, (MifareClassicBlockNumber)block, (const unsigned char*)key.c_str(),
59
                                     (MifareClassicKeyType)keyType) != 0) {
44
                                     (MifareClassicKeyType)keyType) != 0) {
60
         return ResultString::error("Failed to authenticate to MIFARE tag");
45
         return ResultString::error("Failed to authenticate to MIFARE tag");
68
     mifare_classic_disconnect(_tag);
53
     mifare_classic_disconnect(_tag);
69
     return ResultString::ok(std::string((const char*)data, sizeof(data)));
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 View File

20
 
20
 
21
     ResultString readBlock(int sector, int block, std::string key, int keyType);
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
     const freefare_tag * getTag() const;
29
     const freefare_tag * getTag() const;
24
 
30
 
25
     freefare_tag_type getType() const;
31
     freefare_tag_type getType() const;

Loading…
Cancel
Save