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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. LibNfc::~LibNfc()
  11. {
  12. clean();
  13. }
  14. ResultBool LibNfc::init()
  15. {
  16. nfc_init(&_context);
  17. if (!_context) {
  18. return ResultBool::error("LibNfc could not be initialized");
  19. }
  20. return ResultBool::ok(true);
  21. }
  22. std::string LibNfc::getVersion()
  23. {
  24. return nfc_version();
  25. }
  26. void LibNfc::clean()
  27. {
  28. nfc_exit(_context);
  29. _context = 0;
  30. }
  31. Result<std::vector<std::shared_ptr<NfcDevice>>> LibNfc::getDevices() const
  32. {
  33. nfc_connstring devices[16];
  34. memset((char*)devices, 0, sizeof(devices));
  35. size_t count = nfc_list_devices(_context, devices, 16);
  36. if (count < 0) {
  37. return Result<std::vector<std::shared_ptr<NfcDevice>>>::error("Failed to list NFC devices: " + count);
  38. }
  39. std::vector<std::shared_ptr<NfcDevice>> devicesList;
  40. for (size_t i = 0; i < count; ++i) {
  41. devicesList.push_back(std::make_shared<NfcDevice>(this, devices[i]));
  42. }
  43. return Result<std::vector<std::shared_ptr<NfcDevice>>>::ok(devicesList);
  44. }
  45. nfc_context *LibNfc::getContext() const
  46. {
  47. return _context;
  48. }