Browse Source

cli write tag

develop
Robin Thoni 7 years ago
parent
commit
6a43c15862

+ 122
- 37
cli/Interface/MainClass.cpp View File

13
 #include "CommandLineParser.h"
13
 #include "CommandLineParser.h"
14
 #include "MainClass.h"
14
 #include "MainClass.h"
15
 
15
 
16
-#define EX_REDIRECT_ERROR 1
16
+#define EX_OUTPUT_ERROR 1
17
+#define EX_INPUT_ERROR 2
17
 #define EX_KEY_ERROR 10
18
 #define EX_KEY_ERROR 10
18
 #define EX_LIB_NFC_ERROR 12
19
 #define EX_LIB_NFC_ERROR 12
19
 #define EX_NFC_DEVICE_NOT_FOUND 13
20
 #define EX_NFC_DEVICE_NOT_FOUND 13
20
 #define EX_NFC_TAG_NOT_FOUND 14
21
 #define EX_NFC_TAG_NOT_FOUND 14
21
 #define EX_MAP_KEYS_ERROR 15
22
 #define EX_MAP_KEYS_ERROR 15
22
-#define EX_DUMP_ERROR 16
23
+#define EX_READ_ERROR 16
24
+#define EX_WRITE_ERROR 17
23
 
25
 
24
 MainClass::MainClass(int argc, char *argv[])
26
 MainClass::MainClass(int argc, char *argv[])
25
     : _argc(argc)
27
     : _argc(argc)
34
     CommandLineOption optionHelp(&parser, "help", 'h', "Show this help");
36
     CommandLineOption optionHelp(&parser, "help", 'h', "Show this help");
35
 
37
 
36
     CommandLineOption optionMap(&parser, "map", 'm', "Map keys for the tag");
38
     CommandLineOption optionMap(&parser, "map", 'm', "Map keys for the tag");
37
-    CommandLineOption optionDump(&parser, "read", 'r', "Read the tag");
39
+    CommandLineOption optionRead(&parser, "read", 'r', "Read the tag");
40
+    CommandLineOption optionWrite(&parser, "write", 'w', "Write the tag");
38
     CommandLineOption optionDevices(&parser, "devices", 'd', "List NFC devices");
41
     CommandLineOption optionDevices(&parser, "devices", 'd', "List NFC devices");
39
     CommandLineOption optionTags(&parser, "tags", 't', "List NFC tags");
42
     CommandLineOption optionTags(&parser, "tags", 't', "List NFC tags");
40
 
43
 
45
     CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
48
     CommandLineOption optionKey(&parser, "key", 'k', "Key to use to authenticate", "KEY");
46
 
49
 
47
     CommandLineOption optionOutput(&parser, "output", 'o', "Redirect output to FILE. '-' to use stdout", "FILE", "-");
50
     CommandLineOption optionOutput(&parser, "output", 'o', "Redirect output to FILE. '-' to use stdout", "FILE", "-");
51
+    CommandLineOption optionInput(&parser, "input", 'i', "Read input from FILE. '-' to use stdin", "FILE", "-");
48
 
52
 
49
     if (!parser.parse()) {
53
     if (!parser.parse()) {
50
         return parser.showHelp(EX_USAGE);
54
         return parser.showHelp(EX_USAGE);
54
         outputFile = optionOutput.getValue();
58
         outputFile = optionOutput.getValue();
55
     }
59
     }
56
 
60
 
57
-    std::shared_ptr<std::ofstream> fileCout = 0;
58
     if (outputFile != "-" && !outputFile.empty()) {
61
     if (outputFile != "-" && !outputFile.empty()) {
59
-        fileCout = std::make_shared<std::ofstream>();
62
+        std::shared_ptr<std::ofstream> fileCout = std::make_shared<std::ofstream>();
60
         fileCout->open(outputFile);
63
         fileCout->open(outputFile);
61
         if (!*fileCout) {
64
         if (!*fileCout) {
62
             std::cerr << "Failed to redirect output: " << strerror(errno) << std::endl;
65
             std::cerr << "Failed to redirect output: " << strerror(errno) << std::endl;
63
-            return EX_REDIRECT_ERROR;
66
+            return EX_OUTPUT_ERROR;
64
         }
67
         }
65
         _outputStream = fileCout;
68
         _outputStream = fileCout;
66
     }
69
     }
67
 
70
 
71
+    std::string inputFile = optionInput.getDefaultValue();
72
+    if (optionInput.isSet()) {
73
+        inputFile = optionInput.getValue();
74
+    }
75
+    if (inputFile != "-" && !inputFile.empty()) {
76
+        std::shared_ptr<std::ifstream> fileCin = std::make_shared<std::ifstream>();
77
+        fileCin->open(inputFile);
78
+        if (!*fileCin) {
79
+            std::cerr << "Failed to open input file: " << strerror(errno) << std::endl;
80
+            return EX_INPUT_ERROR;
81
+        }
82
+        _inputStream = fileCin;
83
+    }
84
+
68
     if (optionVersion.isSet()) {
85
     if (optionVersion.isSet()) {
69
         printVersion();
86
         printVersion();
70
         return EX_OK;
87
         return EX_OK;
107
         }
124
         }
108
     }
125
     }
109
 
126
 
127
+    std::string inputData = "";
128
+
110
     int res = EX_OK;
129
     int res = EX_OK;
130
+    Actions action;
131
+    if (optionRead.isSet()) {
132
+        action = Read;
133
+    }
134
+    else if (optionMap.isSet()) {
135
+        action = Map;
136
+    }
137
+    else if (optionWrite.isSet()) {
138
+        auto readResult = readStream(cin());
139
+        if (!readResult) {
140
+            readResult.print();
141
+            return EX_INPUT_ERROR;
142
+        }
143
+        for (auto data : readResult.getData()) {
144
+            inputData += data;
145
+        }
146
+        action = Write;
147
+    }
148
+    else {
149
+        std::cerr << "Must select an action (map|read|write|devices|tags)" << std::endl;
150
+        return EX_USAGE;
151
+    }
152
+
111
     LibNfcBusiness libNfc;
153
     LibNfcBusiness libNfc;
112
     auto init = libNfc.init();
154
     auto init = libNfc.init();
113
     if (!init) {
155
     if (!init) {
171
                                 res = EX_NFC_TAG_NOT_FOUND;
213
                                 res = EX_NFC_TAG_NOT_FOUND;
172
                             }
214
                             }
173
                             else {
215
                             else {
174
-                                if (optionDump.isSet()) {
175
-                                    res = dump(tag, keys);
216
+                                if (action == Read) {
217
+                                    res = read(tag, keys);
176
                                 }
218
                                 }
177
-                                else if (optionMap.isSet()) {
219
+                                else if (action == Map) {
178
                                     res = mapKeys(tag, keys);
220
                                     res = mapKeys(tag, keys);
179
                                 }
221
                                 }
180
-                                else {
181
-                                    std::cerr << "Must select an action (map|read|devices|tags)" << std::endl;
182
-                                    res = EX_USAGE;
222
+                                else if (action == Write) {
223
+                                    res = write(tag, keys, inputData);
183
                                 }
224
                                 }
184
                             }
225
                             }
185
                         }
226
                         }
191
         libNfc.clean();
232
         libNfc.clean();
192
     }
233
     }
193
 
234
 
235
+    if (_outputStream != 0) {
236
+        _outputStream->close();
237
+    }
238
+    if (_inputStream != 0) {
239
+        _inputStream->close();
240
+    }
194
     return res;
241
     return res;
195
 }
242
 }
196
 
243
 
216
     return EX_OK;
263
     return EX_OK;
217
 }
264
 }
218
 
265
 
219
-int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
266
+int MainClass::read(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys)
267
+{
268
+    auto readResult = tag->read(keys, printPercentMapKeys, printPercentDump);
269
+    if (!readResult) {
270
+        readResult.print();
271
+        return EX_READ_ERROR;
272
+    }
273
+    auto read = readResult.getData();
274
+    printSectors(read);
275
+    return EX_OK;
276
+}
277
+
278
+int MainClass::write(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys, const std::string &data)
220
 {
279
 {
221
-    auto dumpResult = tag->read(keys, printPercentMapKeys, printPercentDump);
222
-    if (!dumpResult) {
223
-        dumpResult.print();
224
-        return EX_DUMP_ERROR;
280
+    auto writeResult = tag->write(keys, data, false, printPercentMapKeys, printPercentWrite);
281
+    if (!writeResult) {
282
+        writeResult.print();
283
+        return EX_WRITE_ERROR;
225
     }
284
     }
226
-    auto dump = dumpResult.getData();
227
-    for(int s = 0; s < 16; ++s) {
285
+//    std::vector<SectorDbo> sectors;
286
+//    std::string d = StringUtils::ensureSize(data, 1024);
287
+//    for (int i = 0; i < 16; ++i) {
288
+//        SectorDbo sectorDbo(d.substr(i * 64, 64));
289
+//        sectors.push_back(sectorDbo);
290
+//    }
291
+//    printSectors(sectors);
292
+    return EX_OK;
293
+}
294
+
295
+void MainClass::printSectors(const std::vector<SectorDbo> &sectors)
296
+{
297
+    for(int s = 0; s < sectors.size(); ++s) {
228
         cout() << "+Sector: " << s << std::endl;
298
         cout() << "+Sector: " << s << std::endl;
229
-        auto sector = dump[s];
299
+        auto sector = sectors[s];
230
         for (int b = 0; b < 3; ++b) {
300
         for (int b = 0; b < 3; ++b) {
231
             cout() << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
301
             cout() << (sector.hasBlock(b) ? StringUtils::rawToHuman(sector.getBlock(b)) : std::string(32, '-')) << std::endl;
232
         }
302
         }
236
 
306
 
237
 
307
 
238
         cout() << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
308
         cout() << "+Trailer key A: " << (sector.hasKeyA() ? StringUtils::rawToHuman(sector.getKeyA()) : std::string(12, '-'))
239
-            << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
240
-            << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
309
+        << "\t AC bits: " << (sector.hasAccessBits() ? StringUtils::rawToHuman(sector.getAccessBits()) : std::string(8, '-'))
310
+        << "\t key B: " << (sector.hasKeyB() ? StringUtils::rawToHuman(sector.getKeyB()) : std::string(12, '-')) << std::endl;
241
         AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
311
         AccessBitsDbo accessBitsDbo = sector.getAccessBitsDbo();
242
         for (int b = 0; b < 3; ++b) {
312
         for (int b = 0; b < 3; ++b) {
243
             cout() << "+Block: " << b << " ";
313
             cout() << "+Block: " << b << " ";
246
         cout() << "+Block: 3 ";
316
         cout() << "+Block: 3 ";
247
         printTrailerAccessBits(accessBitsDbo);
317
         printTrailerAccessBits(accessBitsDbo);
248
     }
318
     }
249
-    return EX_OK;
250
 }
319
 }
251
 
320
 
252
 void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
321
 void MainClass::printBlockAccessBits(const AccessBitsDbo &accessBits, int block)
290
     printPercent(done, total, "Dumping");
359
     printPercent(done, total, "Dumping");
291
 }
360
 }
292
 
361
 
362
+void MainClass::printPercentWrite(int done, int total)
363
+{
364
+    printPercent(done, total, "Writing");
365
+}
366
+
293
 void MainClass::printVersion()
367
 void MainClass::printVersion()
294
 {
368
 {
295
     cout() << "LibNfc version: " << LibNfcBusiness::getLibNfcVersion() << std::endl;
369
     cout() << "LibNfc version: " << LibNfcBusiness::getLibNfcVersion() << std::endl;
333
 
407
 
334
 Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
408
 Result<std::vector<std::string>> MainClass::readFile(const std::string &filePath)
335
 {
409
 {
336
-    std::vector<std::string> lines;
337
     std::ifstream fileInput(filePath);
410
     std::ifstream fileInput(filePath);
338
     if (fileInput) {
411
     if (fileInput) {
339
-        while (!fileInput.eof()) {
340
-            std::string line;
341
-            std::getline(fileInput, line);
342
-            line = StringUtils::removeSpaces(line);
343
-            if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
344
-                auto keyResult = StringUtils::humanToRaw(line);
345
-                if (!keyResult) {
346
-                    return Result<std::vector<std::string>>::error("Invalid file data");
347
-                }
348
-                line = keyResult.getData();
349
-                lines.push_back(line);
350
-            }
351
-        }
412
+        return readStream(fileInput);
352
     }
413
     }
353
     else {
414
     else {
354
         return Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
415
         return Result<std::vector<std::string>>::error("Failed to open file: " + std::string(strerror(errno)));
355
     }
416
     }
417
+}
418
+
419
+Result<std::vector<std::string>> MainClass::readStream(std::istream &stream)
420
+{
421
+    std::vector<std::string> lines;
422
+    while (!stream.eof()) {
423
+        std::string line;
424
+        std::getline(stream, line);
425
+        line = StringUtils::removeSpaces(line);
426
+        if (line.compare(0, 1, "#") != 0 && line.compare(0, 1, "+") != 0) {
427
+            std::replace(line.begin(), line.end(), '-', '0');
428
+            auto keyResult = StringUtils::humanToRaw(line);
429
+            if (!keyResult) {
430
+                return Result<std::vector<std::string>>::error("Invalid data");
431
+            }
432
+            line = keyResult.getData();
433
+            lines.push_back(line);
434
+        }
435
+    }
356
     return Result<std::vector<std::string>>::ok(lines);
436
     return Result<std::vector<std::string>>::ok(lines);
357
 }
437
 }
358
 
438
 
360
 {
440
 {
361
     return _outputStream == 0 ? std::cout : *_outputStream;
441
     return _outputStream == 0 ? std::cout : *_outputStream;
362
 }
442
 }
443
+
444
+std::istream &MainClass::cin()
445
+{
446
+    return _inputStream == 0 ? std::cin : *_inputStream;
447
+}

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

11
 
11
 
12
 class MainClass {
12
 class MainClass {
13
 public:
13
 public:
14
+    enum Actions {
15
+        Read,
16
+        Write,
17
+        Map
18
+    };
19
+
14
     MainClass(int argc, char* argv[]);
20
     MainClass(int argc, char* argv[]);
15
 
21
 
16
     int main();
22
     int main();
21
 
27
 
22
     int mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys);
28
     int mapKeys(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys);
23
 
29
 
24
-    int dump(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys);
30
+    int read(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys);
31
+
32
+    int write(std::shared_ptr<FreeFareTagBusiness> tag, std::vector<std::string> keys, const std::string& data);
25
 
33
 
26
     Result<std::vector<std::string>> readFile(const std::string& filePath);
34
     Result<std::vector<std::string>> readFile(const std::string& filePath);
27
 
35
 
36
+    Result<std::vector<std::string>> readStream(std::istream& stream);
37
+
38
+    void printSectors(const std::vector<SectorDbo>& sectors);
39
+
28
     void printBlockAccessBits(const AccessBitsDbo& accessBits, int block);
40
     void printBlockAccessBits(const AccessBitsDbo& accessBits, int block);
29
 
41
 
30
     void printTrailerAccessBits(const AccessBitsDbo& accessBits);
42
     void printTrailerAccessBits(const AccessBitsDbo& accessBits);
33
 
45
 
34
     std::ostream& cout();
46
     std::ostream& cout();
35
 
47
 
48
+    std::istream& cin();
49
+
36
     static void printPercent(int done, int total, const std::string& header);
50
     static void printPercent(int done, int total, const std::string& header);
37
 
51
 
38
     static void printPercentMapKeys(int done, int total);
52
     static void printPercentMapKeys(int done, int total);
39
 
53
 
40
     static void printPercentDump(int done, int total);
54
     static void printPercentDump(int done, int total);
41
 
55
 
56
+    static void printPercentWrite(int done, int total);
57
+
42
 private:
58
 private:
43
     int _argc;
59
     int _argc;
44
 
60
 
45
     char** _argv;
61
     char** _argv;
46
 
62
 
47
-    std::shared_ptr<std::ostream> _outputStream;
63
+    std::shared_ptr<std::ofstream> _outputStream;
64
+
65
+    std::shared_ptr<std::ifstream> _inputStream;
48
 
66
 
49
 };
67
 };
50
 
68
 

+ 4
- 4
src/Business/FreeFareTagBusiness.cpp View File

151
 }
151
 }
152
 
152
 
153
 Result<std::vector<SectorDbo>> FreeFareTagBusiness::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
153
 Result<std::vector<SectorDbo>> FreeFareTagBusiness::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
154
-                                                         std::function<void(int, int)> dumpCb)
154
+                                                         std::function<void(int, int)> readCb)
155
 {
155
 {
156
     auto mappedKeysResult = mapKeys(keys, mapCb);
156
     auto mappedKeysResult = mapKeys(keys, mapCb);
157
     if (!mappedKeysResult) {
157
     if (!mappedKeysResult) {
158
         return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
158
         return Result<std::vector<SectorDbo>>::error(mappedKeysResult);
159
     }
159
     }
160
-    return read(mappedKeysResult.getData(), dumpCb);
160
+    return read(mappedKeysResult.getData(), readCb);
161
 }
161
 }
162
 
162
 
163
 ResultBool FreeFareTagBusiness::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
163
 ResultBool FreeFareTagBusiness::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
236
 }
236
 }
237
 
237
 
238
 ResultBool FreeFareTagBusiness::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
238
 ResultBool FreeFareTagBusiness::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
239
-                                      std::function<void(int, int)> mapCb, std::function<void(int, int)> dumpCb)
239
+                                      std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
240
 {
240
 {
241
     auto mappedKeysResult = mapKeys(keys, mapCb);
241
     auto mappedKeysResult = mapKeys(keys, mapCb);
242
     if (!mappedKeysResult) {
242
     if (!mappedKeysResult) {
243
         return ResultBool::error(mappedKeysResult);
243
         return ResultBool::error(mappedKeysResult);
244
     }
244
     }
245
-    return write(mappedKeysResult.getData(), data, writeSector0, dumpCb);
245
+    return write(mappedKeysResult.getData(), data, writeSector0, writeCb);
246
 }
246
 }
247
 
247
 
248
 const std::string &FreeFareTagBusiness::getUid() const
248
 const std::string &FreeFareTagBusiness::getUid() const

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

28
     Result<std::vector<SectorDbo>> read(MappedKeys keys, std::function<void(int, int)> cb = 0);
28
     Result<std::vector<SectorDbo>> read(MappedKeys keys, std::function<void(int, int)> cb = 0);
29
 
29
 
30
     Result<std::vector<SectorDbo>> read(std::vector<std::string> keys, std::function<void(int, int)> mapCb = 0,
30
     Result<std::vector<SectorDbo>> read(std::vector<std::string> keys, std::function<void(int, int)> mapCb = 0,
31
-                                        std::function<void(int, int)> dumpCb = 0);
31
+                                        std::function<void(int, int)> readCb = 0);
32
 
32
 
33
     ResultBool writeBlock(int sector, int block, std::string key, int keyType, const std::string& data);
33
     ResultBool writeBlock(int sector, int block, std::string key, int keyType, const std::string& data);
34
 
34
 
37
     ResultBool write(MappedKeys keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb = 0);
37
     ResultBool write(MappedKeys keys, const std::string& data, bool writeSector0, std::function<void(int, int)> cb = 0);
38
 
38
 
39
     ResultBool write(std::vector<std::string> keys, const std::string& data, bool writeSector0,
39
     ResultBool write(std::vector<std::string> keys, const std::string& data, bool writeSector0,
40
-                     std::function<void(int, int)> mapCb = 0, std::function<void(int, int)> dumpCb = 0);
40
+                     std::function<void(int, int)> mapCb = 0, std::function<void(int, int)> writeCb = 0);
41
 
41
 
42
     const std::string& getUid() const;
42
     const std::string& getUid() const;
43
 
43
 

+ 1
- 1
src/DataAccess/SectorDbo.h View File

46
 protected:
46
 protected:
47
     std::string _blocks[4];
47
     std::string _blocks[4];
48
 
48
 
49
-    bool _haveBlocks[3];
49
+    bool _haveBlocks[4];
50
 
50
 
51
     bool _hasKeyA;
51
     bool _hasKeyA;
52
 
52
 

Loading…
Cancel
Save