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.

LibNfcInternal.cpp 1.4KB

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