You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainClass.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Created by robin on 8/8/15.
  3. //
  4. #include <iostream>
  5. #include <sysexits.h>
  6. #include <DBO/StringUtils.h>
  7. #include <Business/FreeFareDeviceBusiness.h>
  8. #include "DBO/Result.h"
  9. #include "Business/LibNfcBusiness.h"
  10. #include "CommandLineParser.h"
  11. #include "MainClass.h"
  12. MainClass::MainClass(int argc, char *argv[])
  13. : _argc(argc)
  14. , _argv(argv)
  15. {
  16. }
  17. int MainClass::main()
  18. {
  19. std::cout << "LibNfc version: " << LibNfcBusiness::getVersion() << std::endl;
  20. LibNfcBusiness libNfc;
  21. auto init = libNfc.init();
  22. if (!init) {
  23. init.print();
  24. return 1;
  25. }
  26. auto devicesResult = libNfc.getDevices();
  27. if (!devicesResult) {
  28. devicesResult.print();
  29. return 2;
  30. }
  31. auto devices = devicesResult.getData();
  32. if (devices.size() == 0) {
  33. std::cerr << "No NFC device found" << std::endl;
  34. return 3;
  35. }
  36. std::cout << "Found " << devices.size() << " devices: " << std::endl;
  37. for (size_t i = 0; i < devices.size(); ++i) {
  38. std::cout << devices[i]->getConnStr() << std::endl;
  39. }
  40. auto device = devices[0];
  41. auto open = device->open();
  42. if (!open) {
  43. open.print();
  44. return 4;
  45. }
  46. FreeFareDeviceBusiness freeFareDevice(device);
  47. auto tags = freeFareDevice.getTags();
  48. if (!tags) {
  49. tags.print();
  50. return 5;
  51. }
  52. std::cout << "Found " << tags.getData().size() << " tags:" << std::endl;
  53. for (size_t i = 0; i < tags.getData().size(); ++i) {
  54. auto tag = tags.getData()[i];
  55. std::cout << "UID: " << tag->getUid() << std::endl;
  56. std::cout << "Type: " << tag->getType() << std::endl;
  57. mapKeys(tag);
  58. }
  59. device->close();
  60. libNfc.clean();
  61. return 0;
  62. }
  63. int MainClass::mapKeys(std::shared_ptr<FreeFareTagBusiness> tag)
  64. {
  65. std::vector<std::string> keys;
  66. keys.push_back(StringUtils::humanToRaw("8829da9daf76").getData());
  67. auto mappedKeysResult = tag->mapKeys(keys);
  68. if (!mappedKeysResult) {
  69. mappedKeysResult.print();
  70. }
  71. else {
  72. auto mappedKeys = mappedKeysResult.getData();
  73. for (int s = 0; s < mappedKeys.size(); ++s) {
  74. auto sectorKeys = mappedKeys[s];
  75. std::cout << "+Sector: " << s << std::endl;
  76. for (int b = 0; b < sectorKeys.size(); ++b) {
  77. auto blockKeys = sectorKeys[b];
  78. std::cout << "+Block: " << b << std::endl;
  79. std::cout << "Key A: " << StringUtils::rawToHuman(blockKeys.first) << std::endl;
  80. std::cout << "Key B: " << StringUtils::rawToHuman(blockKeys.second) << std::endl;
  81. }
  82. }
  83. }
  84. return 0;
  85. }
  86. int MainClass::dump(std::shared_ptr<FreeFareTagBusiness> tag)
  87. {
  88. for(int s = 0; s < 16; ++s)
  89. {
  90. std::cout << "+Sector: " << s << std::endl;
  91. auto sectorResult = tag->readSector(s, StringUtils::humanToRaw("8829da9daf76").getData(), MFC_KEY_A);
  92. if (!sectorResult)
  93. {
  94. sectorResult.print();
  95. }
  96. else
  97. {
  98. auto sector = sectorResult.getData();
  99. for (int b = 0; b < 3; ++b)
  100. {
  101. std::cout << StringUtils::rawToHuman(sector.getBlock(b)) << std::endl;
  102. }
  103. std::cout << StringUtils::rawToHuman(sector.getKeyA()) << " "
  104. << StringUtils::rawToHuman(sector.getAccessBits()) << " "
  105. << StringUtils::rawToHuman(sector.getKeyB()) << std::endl;
  106. }
  107. }
  108. return 0;
  109. }