Browse Source

updated callback system

develop
Robin Thoni 7 years ago
parent
commit
fc8503f4d6

+ 25
- 7
libnfc_cpptools/inc/libnfc_cpptools/FreeFareTagClassic.h View File

14
 
14
 
15
 class FreeFareTagClassicInternal;
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
 class FreeFareTagClassic : public FreeFareTag
35
 class FreeFareTagClassic : public FreeFareTag
18
 {
36
 {
19
 public:
37
 public:
22
     LibNfc::Utils::ResultBool authenticate(int sector, const std::string& key, int keyType);
40
     LibNfc::Utils::ResultBool authenticate(int sector, const std::string& key, int keyType);
23
 
41
 
24
     LibNfc::Utils::Result<MappedKeys> mapKeys(const std::vector<std::string>& keys,
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
     LibNfc::Utils::ResultString readBlock(int sector, int block, const std::string& key, int keyType);
45
     LibNfc::Utils::ResultString readBlock(int sector, int block, const std::string& key, int keyType);
28
 
46
 
29
     LibNfc::Utils::Result<FreeFareClassicSector> readSector(int sector, const std::string& key, int keyType);
47
     LibNfc::Utils::Result<FreeFareClassicSector> readSector(int sector, const std::string& key, int keyType);
30
 
48
 
31
     LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> read(const MappedKeys& keys,
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
     LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> read(const std::vector<std::string>& keys,
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
     LibNfc::Utils::ResultBool writeBlock(int sector,
56
     LibNfc::Utils::ResultBool writeBlock(int sector,
39
                                          int block,
57
                                          int block,
46
     LibNfc::Utils::ResultBool write(const MappedKeys& keys,
64
     LibNfc::Utils::ResultBool write(const MappedKeys& keys,
47
                                     const std::string& data,
65
                                     const std::string& data,
48
                                     bool writeSector0,
66
                                     bool writeSector0,
49
-                                    std::function<void(int, int)> cb = nullptr);
67
+                                    std::function<void(FreeFareTagClassicWriteProgress)> writeCb = nullptr);
50
 
68
 
51
     LibNfc::Utils::ResultBool write(const std::vector<std::string>& keys,
69
     LibNfc::Utils::ResultBool write(const std::vector<std::string>& keys,
52
                                     const std::string& data,
70
                                     const std::string& data,
53
                                     bool writeSector0,
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
 }; // FreeFare
76
 }; // FreeFare

+ 35
- 30
libnfc_cpptools/src/freefare/Tags/Classic/FreeFareTagClassic.cpp View File

21
     return std::static_pointer_cast<FreeFareTagClassicInternal>(_tag)->authenticate(sector, key, keyType);
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
     MappedKeys mappedKeys;
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
     for (int i = 0; i < 16; ++i) {
31
     for (int i = 0; i < 16; ++i) {
31
         std::pair<std::string, std::string> blockKeys;
32
         std::pair<std::string, std::string> blockKeys;
37
             if (authenticate(i, key, MFC_KEY_B)) {
38
             if (authenticate(i, key, MFC_KEY_B)) {
38
                 blockKeys.second = key;
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
             if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
44
             if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
44
                 break;
45
                 break;
46
         }
47
         }
47
         mappedKeys.push_back(blockKeys);
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
     return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
54
     return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
74
     return LibNfc::Utils::Result<FreeFareClassicSector>::ok(FreeFareClassicSector(res));
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
     if (keys.size() != 16) {
81
     if (keys.size() != 16) {
80
         return LibNfc::Utils::Result<std::vector<FreeFareClassicSector>>::error("Must have 16 sectors keys");
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
     std::vector<FreeFareClassicSector> sectors;
86
     std::vector<FreeFareClassicSector> sectors;
85
     for (int s = 0; s < keys.size(); ++s) {
87
     for (int s = 0; s < keys.size(); ++s) {
86
         auto sectorKey = keys[s];
88
         auto sectorKey = keys[s];
88
         bool keyA = false;
90
         bool keyA = false;
89
         bool keyB = false;
91
         bool keyB = false;
90
         for (int b = 0; b < 3; ++b) {
92
         for (int b = 0; b < 3; ++b) {
91
-            std::string data = "";
93
+            std::string data;
92
             if (!sectorKey.first.empty()) {
94
             if (!sectorKey.first.empty()) {
93
                 auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
95
                 auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
94
                 if (blockResult) {
96
                 if (blockResult) {
106
                 }
108
                 }
107
             }
109
             }
108
             sector.setBlock(b, data);
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
         int b = 3;
115
         int b = 3;
127
                 keyB = true;
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
         sector.setBlock(b, dataA);
136
         sector.setBlock(b, dataA);
149
 
151
 
150
         sectors.push_back(sector);
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
     return LibNfc::Utils::Result<std::vector<FreeFareClassicSector>>::ok(sectors);
158
     return LibNfc::Utils::Result<std::vector<FreeFareClassicSector>>::ok(sectors);
157
 }
159
 }
158
 
160
 
159
 LibNfc::Utils::Result<std::vector<FreeFareClassicSector>> FreeFareTagClassic::read(const std::vector<std::string>& keys,
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
     auto mappedKeysResult = mapKeys(keys, mapCb);
165
     auto mappedKeysResult = mapKeys(keys, mapCb);
164
     if (!mappedKeysResult) {
166
     if (!mappedKeysResult) {
190
 }
192
 }
191
 
193
 
192
 LibNfc::Utils::ResultBool FreeFareTagClassic::write(const MappedKeys& keys,
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
     if (keys.size() != 16) {
199
     if (keys.size() != 16) {
198
         return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
200
         return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
199
     }
201
     }
200
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
202
     std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
201
     std::string errors;
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
     for (unsigned long s = 0; s < keys.size(); ++s) {
206
     for (unsigned long s = 0; s < keys.size(); ++s) {
205
         auto sectorKey = keys[s];
207
         auto sectorKey = keys[s];
206
         for (int b = 0; b < 4; ++b) {
208
         for (int b = 0; b < 4; ++b) {
208
                 continue;
210
                 continue;
209
             }
211
             }
210
             std::string blockData = d.substr((s * 64) + (b * 16), 16);
212
             std::string blockData = d.substr((s * 64) + (b * 16), 16);
211
-            if (cb != nullptr && done < total) {
213
+            if (writeCb != nullptr && done < total) {
212
                 bool keyA = false;
214
                 bool keyA = false;
213
                 bool keyB = false;
215
                 bool keyB = false;
214
                 std::string sectorErrors;
216
                 std::string sectorErrors;
235
                 if (!keyA && !keyB) {
237
                 if (!keyA && !keyB) {
236
                     errors += std::string(errors.empty() ? "" : "\n") + sectorErrors;
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
     if (errors.empty()) {
247
     if (errors.empty()) {
246
         return LibNfc::Utils::ResultBool::ok(true);
248
         return LibNfc::Utils::ResultBool::ok(true);
248
     return LibNfc::Utils::ResultBool::error(errors);
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
     auto mappedKeysResult = mapKeys(keys, mapCb);
259
     auto mappedKeysResult = mapKeys(keys, mapCb);
255
     if (!mappedKeysResult) {
260
     if (!mappedKeysResult) {

Loading…
Cancel
Save