Explicit open() and close() on device.

This commit is contained in:
Ondrej Mikle 2014-07-19 14:56:15 +02:00
parent a4d676772e
commit bc9630c927
2 changed files with 39 additions and 10 deletions

View file

@ -8,18 +8,33 @@
using namespace std;
NFCDevice::NFCDevice()
NFCDevice::NFCDevice():
pollNr(20),
pollPeriod(2),
_nfcContext(NULL),
_nfcDevice(NULL),
_opened(false)
{
pollNr = 20;
pollPeriod = 2;
_nfcContext = NULL;
nfc_init(&_nfcContext);
if (_nfcContext == NULL) {
throw NFCError("Unable to init libnfc (malloc)");
}
open();
}
NFCDevice::~NFCDevice()
{
close();
nfc_exit(_nfcContext);
}
void NFCDevice::open()
{
if (opened()) {
return;
}
_nfcDevice = nfc_open(_nfcContext, NULL);
if (_nfcDevice == NULL) {
@ -27,16 +42,22 @@ NFCDevice::NFCDevice()
}
if (nfc_initiator_init(_nfcDevice) < 0) {
nfc_close(_nfcDevice);
close();
throw NFCError("NFC initiator error");
}
_opened = true;
}
NFCDevice::~NFCDevice()
void NFCDevice::close()
{
//nfc_close(_nfcDevice);
nfc_exit(_nfcContext);
if (!opened()) {
return;
}
nfc_close(_nfcDevice);
_nfcDevice = NULL;
_opened = false;
}
std::string NFCDevice::scanUID()

View file

@ -18,6 +18,12 @@ public:
std::string scanUID();
void open();
bool opened() const {return _opened;}
void close();
uint8_t pollNr;
uint8_t pollPeriod;
@ -32,6 +38,8 @@ protected:
nfc_device *_nfcDevice;
bool _opened;
};