Browse Source

added progress callback

develop
Robin Thoni 7 years ago
parent
commit
6f25aa8f79

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

6
 #include <sysexits.h>
6
 #include <sysexits.h>
7
 #include <DBO/StringUtils.h>
7
 #include <DBO/StringUtils.h>
8
 #include <Business/FreeFareDeviceBusiness.h>
8
 #include <Business/FreeFareDeviceBusiness.h>
9
+#include <iomanip>
9
 #include "DBO/Result.h"
10
 #include "DBO/Result.h"
10
 #include "Business/LibNfcBusiness.h"
11
 #include "Business/LibNfcBusiness.h"
11
 #include "CommandLineParser.h"
12
 #include "CommandLineParser.h"
80
 //    keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
81
 //    keys.push_back(StringUtils::humanToRaw("ffffffffffff").getData());
81
     keys.push_back(StringUtils::humanToRaw("484558414354").getData());
82
     keys.push_back(StringUtils::humanToRaw("484558414354").getData());
82
 
83
 
83
-    int res = dump(tag, keys); //mapKeys(tag);
84
+    int res = dump(tag, keys);
85
+//    int res = mapKeys(tag, keys);
84
 
86
 
85
     device->close();
87
     device->close();
86
     libNfc.clean();
88
     libNfc.clean();
90
 
92
 
91
 int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
93
 int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
92
 {
94
 {
93
-    auto mappedKeysResult = tag->mapKeys(keys);
95
+    auto mappedKeysResult = tag->mapKeys(keys, printPercent);
96
+    std::cout << "\r";
94
     if (!mappedKeysResult) {
97
     if (!mappedKeysResult) {
95
         mappedKeysResult.print();
98
         mappedKeysResult.print();
96
     }
99
     }
112
 
115
 
113
 int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
116
 int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
114
 {
117
 {
115
-    auto dumpResult = tag->dump(keys);
118
+    auto dumpResult = tag->dump(keys, printPercent, printPercent);
119
+    std::cout << "\r";
116
     if (!dumpResult) {
120
     if (!dumpResult) {
117
         dumpResult.print();
121
         dumpResult.print();
118
         return 7;
122
         return 7;
157
     std::cout << " key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : "") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : "");
161
     std::cout << " key B read: " << (accessBits.canKeyAReadKeyBTrailer() ? "A" : "") << (accessBits.canKeyBReadKeyBTrailer() ? "B" : "");
158
     std::cout << " key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : "") << std::endl;;
162
     std::cout << " key B write: " << (accessBits.canKeyAWriteKeyBTrailer() ? "A" : "") << (accessBits.canKeyBWriteKeyBTrailer() ? "B" : "") << std::endl;;
159
 }
163
 }
164
+
165
+void MainClass::printPercent(int done, int total)
166
+{
167
+    std::cout << "\r" << std::fixed << std::setprecision(1) <<  ((float)done / (float)total * 100.0) << "%" << std::flush;
168
+}

+ 2
- 0
cli/Interface/MainClass.h View File

22
 
22
 
23
     void printTrailerAccessBits(const AccessBitsDbo& accessBits);
23
     void printTrailerAccessBits(const AccessBitsDbo& accessBits);
24
 
24
 
25
+    static void printPercent(int done, int total);
26
+
25
 private:
27
 private:
26
     int _argc;
28
     int _argc;
27
 
29
 

+ 16
- 6
src/Business/FreeFareTagBusiness.cpp View File

50
     return _tag;
50
     return _tag;
51
 }
51
 }
52
 
52
 
53
-Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys)
53
+Result<MappedKeys> FreeFareTagBusiness::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
54
 {
54
 {
55
     std::vector<std::vector<std::pair<std::string, std::string>>> mappedKeys;
55
     std::vector<std::vector<std::pair<std::string, std::string>>> mappedKeys;
56
 
56
 
58
         std::vector<std::pair<std::string, std::string>> sectorKeys;
58
         std::vector<std::pair<std::string, std::string>> sectorKeys;
59
         for (int j = 0; j < 4; ++j) {
59
         for (int j = 0; j < 4; ++j) {
60
             std::pair<std::string, std::string> blockKeys;
60
             std::pair<std::string, std::string> blockKeys;
61
-            for (auto key : keys) {
61
+            for (int k = 0; k < keys.size(); ++k) {
62
+                auto key = keys[k];
62
                 if (authenticate(i, j, key, MFC_KEY_A)) {
63
                 if (authenticate(i, j, key, MFC_KEY_A)) {
63
                     blockKeys.first = key;
64
                     blockKeys.first = key;
64
                 }
65
                 }
65
                 if (authenticate(i, j, key, MFC_KEY_B)) {
66
                 if (authenticate(i, j, key, MFC_KEY_B)) {
66
                     blockKeys.second = key;
67
                     blockKeys.second = key;
67
                 }
68
                 }
69
+                if (cb != 0) {
70
+                    cb((i * 4 * keys.size()) + (j * keys.size()) + k + 1, 16 * 4 * keys.size());
71
+                }
68
                 if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
72
                 if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
69
                     break;
73
                     break;
70
                 }
74
                 }
77
     return Result<std::vector<std::vector<std::pair<std::string, std::string>>>>::ok(mappedKeys);
81
     return Result<std::vector<std::vector<std::pair<std::string, std::string>>>>::ok(mappedKeys);
78
 }
82
 }
79
 
83
 
80
-Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys)
84
+Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(MappedKeys keys, std::function<void(int, int)> cb)
81
 {
85
 {
82
     if (keys.size() != 16) {
86
     if (keys.size() != 16) {
83
         return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
87
         return Result<std::vector<SectorDbo>>::error("Must have 16 sectors");
112
                 }
116
                 }
113
             }
117
             }
114
             sector.setBlock(b, data);
118
             sector.setBlock(b, data);
119
+            if (cb != 0) {
120
+                cb((s * sectorKeys.size()) + b + 1, keys.size() * sectorKeys.size());
121
+            }
115
         }
122
         }
116
         int b = sectorKeys.size() - 1;
123
         int b = sectorKeys.size() - 1;
117
         auto blockKey = sectorKeys[b];
124
         auto blockKey = sectorKeys[b];
132
         }
139
         }
133
         sector.setBlock(b, data);
140
         sector.setBlock(b, data);
134
         AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
141
         AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
142
+        if (cb != 0) {
143
+            cb((s * sectorKeys.size()) + b + 1, keys.size() * sectorKeys.size());
144
+        }
135
 
145
 
136
         sector.setKeyA(keyA ? blockKey.first : "");
146
         sector.setKeyA(keyA ? blockKey.first : "");
137
         sector.setKeyB(keyB ? blockKey.second : "");
147
         sector.setKeyB(keyB ? blockKey.second : "");
150
     return Result<std::vector<SectorDbo>>::ok(sectors);
160
     return Result<std::vector<SectorDbo>>::ok(sectors);
151
 }
161
 }
152
 
162
 
153
-Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(std::vector<std::string> keys)
163
+Result<std::vector<SectorDbo>> FreeFareTagBusiness::dump(std::vector<std::string> keys, std::function<void(int, int)> mapCb, std::function<void(int, int)> dumpCb)
154
 {
164
 {
155
-    auto mappedKeysResult = mapKeys(keys);
165
+    auto mappedKeysResult = mapKeys(keys, mapCb);
156
     if (!mappedKeysResult) {
166
     if (!mappedKeysResult) {
157
         return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
167
         return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
158
     }
168
     }
159
-    return dump(mappedKeysResult.getData());
169
+    return dump(mappedKeysResult.getData(), dumpCb);
160
 }
170
 }

+ 3
- 3
src/Business/FreeFareTagBusiness.h View File

23
 
23
 
24
     Result<SectorDbo> readSector(int sector, std::string key, int keyType);
24
     Result<SectorDbo> readSector(int sector, std::string key, int keyType);
25
 
25
 
26
-    Result<MappedKeys> mapKeys(std::vector<std::string> keys);
26
+    Result<MappedKeys> mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb = 0);
27
 
27
 
28
-    Result<std::vector<SectorDbo>> dump(MappedKeys keys);
28
+    Result<std::vector<SectorDbo>> dump(MappedKeys keys, std::function<void(int, int)> cb  = 0);
29
 
29
 
30
-    Result<std::vector<SectorDbo>> dump(std::vector<std::string> keys);
30
+    Result<std::vector<SectorDbo>> dump(std::vector<std::string> keys, std::function<void(int, int)> mapCb = 0, std::function<void(int, int)> dumpCb = 0);
31
 
31
 
32
     const std::string& getUid() const;
32
     const std::string& getUid() const;
33
 
33
 

Loading…
Cancel
Save