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.

LibNfc.cpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Created by robin on 7/22/16.
  3. //
  4. #include <algorithm>
  5. #include "libnfc_cpptools/LibNfc.h"
  6. #include "LibNfcInternal.h"
  7. #define Q(x) #x
  8. #define QUOTE(x) Q(x)
  9. #ifndef GIT_SHA1
  10. #define GIT_SHA1 "unknown"
  11. #endif
  12. #ifndef GIT_REF_NAME
  13. #define GIT_REF_NAME "unknown"
  14. #endif
  15. namespace LibNfc
  16. {
  17. namespace Core
  18. {
  19. LibNfcContext::LibNfcContext()
  20. {
  21. _libNfc = std::make_shared<LibNfcInternal>();
  22. }
  23. LibNfcContext::~LibNfcContext()
  24. {
  25. _libNfc->clean();
  26. }
  27. LibNfc::Utils::ResultBool LibNfcContext::init()
  28. {
  29. if (isInitialized()) {
  30. return LibNfc::Utils::ResultBool::error("LibNfc is already initialized");
  31. }
  32. return _libNfc->init();
  33. }
  34. std::string LibNfcContext::getLibNfcVersion()
  35. {
  36. return LibNfcInternal::getVersion();
  37. }
  38. void LibNfcContext::clean()
  39. {
  40. if (isInitialized()) {
  41. _libNfc->clean();
  42. }
  43. }
  44. bool LibNfcContext::isInitialized() const
  45. {
  46. return _libNfc->getContext() != 0;
  47. }
  48. LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfcContext::getDevices() const
  49. {
  50. if (!isInitialized()) {
  51. return LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>>::error("LibNfc is not initialized");
  52. }
  53. auto devicesResult = _libNfc->getDevices();
  54. if (!devicesResult) {
  55. return LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>>::error(devicesResult);
  56. }
  57. auto devices = devicesResult.getData();
  58. std::vector<std::shared_ptr<NfcDevice>> devicesBusiness;
  59. for (auto device : devices) {
  60. devicesBusiness.push_back(std::make_shared<NfcDevice>(device));
  61. }
  62. return LibNfc::Utils::Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesBusiness);
  63. }
  64. std::string LibNfcContext::getMifareToolsVersion()
  65. {
  66. return QUOTE(GIT_REF_NAME) "-" QUOTE(GIT_SHA1);
  67. }
  68. }; // Core
  69. }; // LibNfc