Procházet zdrojové kódy

updated callback system

develop
Robin Thoni před 7 roky
rodič
revize
fc8503f4d6

+ 25
- 7
libnfc_cpptools/inc/libnfc_cpptools/FreeFareTagClassic.h Zobrazit soubor

@@ -14,6 +14,24 @@ namespace FreeFare
14 14
 
15 15
 class FreeFareTagClassicInternal;
16 16
 
17
+struct FreeFareTagClassicMappingProgress
18
+{
19
+    int32_t done;
20
+    int32_t total;
21
+};
22
+
23
+struct FreeFareTagClassicReadProgress
24
+{
25
+    int32_t done;
26
+    int32_t total;
27
+};
28
+
29
+struct FreeFareTagClassicWriteProgress
30
+{
31
+    int32_t done;
32
+    int32_t total;
33
+};
34
+
17 35
 class FreeFareTagClassic : public FreeFareTag
18 36
 {
19 37
 public:
@@ -22,18 +40,18 @@ public:
22 40
     LibNfc::Utils::ResultBool authenticate(int sector, const std::string& key, int keyType);
23 41
 
24 42
     LibNfc::Utils::Result<MappedKeys> mapKeys(const std::vector<std::string>& keys,
25
-                                              std::function<void(int, int)> cb = nullptr);
43
+                                              std::function<void(FreeFareTagClassicMappingProgress)> mapCb = nullptr);
26 44
 
27 45
     LibNfc::Utils::ResultString readBlock(int sector, int block, const std::string& key, int keyType);
28 46
 
29 47
     LibNfc::Utils::Result<FreeFareClassicSector> readSector(int sector, const std::string& key, int keyType);
30 48
 
31 49
     LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> read(const MappedKeys& keys,
32
-                                                            std::function<void(int, int)> cb = nullptr);
50
+                                                            std::function<void(FreeFareTagClassicReadProgress)> readCb = nullptr);
33 51
 
34 52
     LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> read(const std::vector<std::string>& keys,
35
-                                                            std::function<void(int, int)> mapCb = nullptr,
36
-                                                            std::function<void(int, int)> readCb = nullptr);
53
+                                                            std::function<void(FreeFareTagClassicMappingProgress)> mapCb = nullptr,
54
+                                                            std::function<void(FreeFareTagClassicReadProgress)> readCb = nullptr);
37 55
 
38 56
     LibNfc::Utils::ResultBool writeBlock(int sector,
39 57
                                          int block,
@@ -46,13 +64,13 @@ public:
46 64
     LibNfc::Utils::ResultBool write(const MappedKeys& keys,
47 65
                                     const std::string& data,
48 66
                                     bool writeSector0,
49
-                                    std::function<void(int, int)> cb = nullptr);
67
+                                    std::function<void(FreeFareTagClassicWriteProgress)> writeCb = nullptr);
50 68
 
51 69
     LibNfc::Utils::ResultBool write(const std::vector<std::string>& keys,
52 70
                                     const std::string& data,
53 71
                                     bool writeSector0,
54
-                                    std::function<void(int, int)> mapCb = nullptr,
55
-                                    std::function<void(int, int)> writeCb = nullptr);
72
+                                    std::function<void(FreeFareTagClassicMappingProgress)> mapCb = nullptr,
73
+                                    std::function<void(FreeFareTagClassicWriteProgress)> writeCb = nullptr);
56 74
 };
57 75
 
58 76
 }; // FreeFare

+ 35
- 30
libnfc_cpptools/src/freefare/Tags/Classic/FreeFareTagClassic.cpp Zobrazit soubor

@@ -21,11 +21,12 @@ LibNfc::Utils::ResultBool FreeFareTagClassic::authenticate(int sector, const std
21 21
     return std::static_pointer_cast<FreeFareTagClassicInternal>(_tag)->authenticate(sector, key, keyType);
22 22
 }
23 23
 
24
-LibNfc::Utils::Result<MappedKeys> FreeFareTagClassic::mapKeys(const std::vector<std::string>& keys, std::function<void(int, int)> cb)
24
+LibNfc::Utils::Result<MappedKeys> FreeFareTagClassic::mapKeys(const std::vector<std::string>& keys,
25
+                                                              std::function<void(FreeFareTagClassicMappingProgress)> mapCb)
25 26
 {
26 27
     MappedKeys mappedKeys;
27
-    unsigned long done = 0;
28
-    unsigned long total = 16 * keys.size();
28
+    int32_t done = 0;
29
+    int32_t total = 16 * keys.size();
29 30
 
30 31
     for (int i = 0; i < 16; ++i) {
31 32
         std::pair<std::string, std::string> blockKeys;
@@ -37,8 +38,8 @@ LibNfc::Utils::Result<MappedKeys> FreeFareTagClassic::mapKeys(const std::vector<
37 38
             if (authenticate(i, key, MFC_KEY_B)) {
38 39
                 blockKeys.second = key;
39 40
             }
40
-            if (cb != nullptr) {
41
-                cb(++done, total);
41
+            if (mapCb != nullptr) {
42
+                mapCb({++done, total});
42 43
             }
43 44
             if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
44 45
                 break;
@@ -46,8 +47,8 @@ LibNfc::Utils::Result<MappedKeys> FreeFareTagClassic::mapKeys(const std::vector<
46 47
         }
47 48
         mappedKeys.push_back(blockKeys);
48 49
     }
49
-    if (cb != nullptr && done < total) {
50
-        cb(total, total);
50
+    if (mapCb != nullptr && done < total) {
51
+        mapCb({total, total});
51 52
     }
52 53
 
53 54
     return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
@@ -74,13 +75,14 @@ LibNfc::Utils::Result<FreeFareClassicSector> FreeFareTagClassic::readSector(int
74 75
     return LibNfc::Utils::Result<FreeFareClassicSector>::ok(FreeFareClassicSector(res));
75 76
 }
76 77
 
77
-LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::read(const MappedKeys& keys, std::function<void(int, int)> cb)
78
+LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::read(const MappedKeys& keys,
79
+                                                                                   std::function<void(FreeFareTagClassicReadProgress)> readCb)
78 80
 {
79 81
     if (keys.size() != 16) {
80 82
         return LibNfc::Utils::Result<std::vector<FreeFareClassicSector>>::error("Must have 16 sectors keys");
81 83
     }
82
-    int done = 0;
83
-    int total = 4 * keys.size();
84
+    int32_t done = 0;
85
+    int32_t total = 4 * keys.size();
84 86
     std::vector<FreeFareClassicSector> sectors;
85 87
     for (int s = 0; s < keys.size(); ++s) {
86 88
         auto sectorKey = keys[s];
@@ -88,7 +90,7 @@ LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::re
88 90
         bool keyA = false;
89 91
         bool keyB = false;
90 92
         for (int b = 0; b < 3; ++b) {
91
-            std::string data = "";
93
+            std::string data;
92 94
             if (!sectorKey.first.empty()) {
93 95
                 auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
94 96
                 if (blockResult) {
@@ -106,8 +108,8 @@ LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::re
106 108
                 }
107 109
             }
108 110
             sector.setBlock(b, data);
109
-            if (cb != nullptr) {
110
-                cb(++done, total);
111
+            if (readCb != nullptr) {
112
+                readCb({++done, total});
111 113
             }
112 114
         }
113 115
         int b = 3;
@@ -127,8 +129,8 @@ LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::re
127 129
                 keyB = true;
128 130
             }
129 131
         }
130
-        if (cb != nullptr) {
131
-            cb(++done, total);
132
+        if (readCb != nullptr) {
133
+            readCb({++done, total});
132 134
         }
133 135
 
134 136
         sector.setBlock(b, dataA);
@@ -149,16 +151,16 @@ LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::re
149 151
 
150 152
         sectors.push_back(sector);
151 153
     }
152
-    if (cb != nullptr && done < total) {
153
-        cb(total, total);
154
+    if (readCb != nullptr && done < total) {
155
+        readCb({total, total});
154 156
     }
155 157
 
156 158
     return LibNfc::Utils::Result<std::vector<FreeFareClassicSector>>::ok(sectors);
157 159
 }
158 160
 
159 161
 LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::read(const std::vector<std::string>& keys,
160
-                                                                     std::function<void(int, int)> mapCb,
161
-                                                                     std::function<void(int, int)> readCb)
162
+                                                                     std::function<void(FreeFareTagClassicMappingProgress)> mapCb,
163
+                                                                     std::function<void(FreeFareTagClassicReadProgress)> readCb)
162 164
 {
163 165
     auto mappedKeysResult = mapKeys(keys, mapCb);
164 166
     if (!mappedKeysResult) {
@@ -190,17 +192,17 @@ LibNfc::Utils::ResultBool FreeFareTagClassic::writeSector(int sector, const std:
190 192
 }
191 193
 
192 194
 LibNfc::Utils::ResultBool FreeFareTagClassic::write(const MappedKeys& keys,
193
-                                             const std::string& data,
194
-                                             bool writeSector0,
195
-                                             std::function<void(int, int)> cb)
195
+                                                    const std::string& data,
196
+                                                    bool writeSector0,
197
+                                                    std::function<void(FreeFareTagClassicWriteProgress)> writeCb)
196 198
 {
197 199
     if (keys.size() != 16) {
198 200
         return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
199 201
     }
200 202
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
201 203
     std::string errors;
202
-    unsigned long done = 0;
203
-    unsigned long total = 4 * keys.size();
204
+    int32_t done = 0;
205
+    int32_t total = 4 * keys.size();
204 206
     for (unsigned long s = 0; s < keys.size(); ++s) {
205 207
         auto sectorKey = keys[s];
206 208
         for (int b = 0; b < 4; ++b) {
@@ -208,7 +210,7 @@ LibNfc::Utils::ResultBool FreeFareTagClassic::write(const MappedKeys& keys,
208 210
                 continue;
209 211
             }
210 212
             std::string blockData = d.substr((s * 64) + (b * 16), 16);
211
-            if (cb != nullptr && done < total) {
213
+            if (writeCb != nullptr && done < total) {
212 214
                 bool keyA = false;
213 215
                 bool keyB = false;
214 216
                 std::string sectorErrors;
@@ -235,12 +237,12 @@ LibNfc::Utils::ResultBool FreeFareTagClassic::write(const MappedKeys& keys,
235 237
                 if (!keyA && !keyB) {
236 238
                     errors += std::string(errors.empty() ? "" : "\n") + sectorErrors;
237 239
                 }
238
-                cb(++done, total);
240
+                writeCb({++done, total});
239 241
             }
240 242
         }
241 243
     }
242
-    if (cb != nullptr && done < total) {
243
-        cb(total, total);
244
+    if (writeCb != nullptr && done < total) {
245
+        writeCb({total, total});
244 246
     }
245 247
     if (errors.empty()) {
246 248
         return LibNfc::Utils::ResultBool::ok(true);
@@ -248,8 +250,11 @@ LibNfc::Utils::ResultBool FreeFareTagClassic::write(const MappedKeys& keys,
248 250
     return LibNfc::Utils::ResultBool::error(errors);
249 251
 }
250 252
 
251
-LibNfc::Utils::ResultBool FreeFareTagClassic::write(const std::vector<std::string>& keys, const std::string& data, bool writeSector0,
252
-                                             std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
253
+LibNfc::Utils::ResultBool FreeFareTagClassic::write(const std::vector<std::string>& keys,
254
+                                                    const std::string& data,
255
+                                                    bool writeSector0,
256
+                                                    std::function<void(FreeFareTagClassicMappingProgress)> mapCb,
257
+                                                    std::function<void(FreeFareTagClassicWriteProgress)> writeCb)
253 258
 {
254 259
     auto mappedKeysResult = mapKeys(keys, mapCb);
255 260
     if (!mappedKeysResult) {

Načítá se…
Zrušit
Uložit