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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // Created by robin on 6/19/16.
  3. //
  4. #include <boost/shared_ptr.hpp>
  5. #include "LibNfc.h"
  6. LibNfc::LibNfc()
  7. : _context(0)
  8. {
  9. }
  10. ResultBool LibNfc::init()
  11. {
  12. if (isInitialized()) {
  13. return ResultBool::error("LibNfc is already initialized");
  14. }
  15. nfc_init(&_context);
  16. if (!_context) {
  17. return ResultBool::error("LibNfc could not be initialized");
  18. }
  19. return ResultBool::ok(true);
  20. }
  21. std::string LibNfc::getVersion()
  22. {
  23. return nfc_version();
  24. }
  25. LibNfc::~LibNfc()
  26. {
  27. clean();
  28. }
  29. bool LibNfc::isInitialized()
  30. {
  31. return _context != 0;
  32. }
  33. void LibNfc::clean()
  34. {
  35. if (isInitialized()) {
  36. nfc_exit(_context);
  37. _context = 0;
  38. }
  39. }
  40. Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfc::getDevices()
  41. {
  42. if (!isInitialized()) {
  43. return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("LibNfc is not initialized");
  44. }
  45. nfc_connstring devices[16];
  46. size_t count = nfc_list_devices(_context, devices, sizeof(devices));
  47. if (count < 0) {
  48. return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("Failed to list NFC devices");
  49. }
  50. std::vector<std::shared_ptr<NfcDevice>> devicesList;
  51. for (size_t i = 0; i < count; ++i) {
  52. devicesList.push_back(std::make_shared<NfcDevice>(this, devices[i]));
  53. }
  54. return Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesList);
  55. }
  56. nfc_context *LibNfc::getContext() const
  57. {
  58. return _context;
  59. }