mirror of
https://github.com/brmlab/brmdoor_libnfc.git
synced 2025-06-08 08:34:00 +02:00
Explicit open() and close() on device.
This commit is contained in:
parent
a4d676772e
commit
bc9630c927
2 changed files with 39 additions and 10 deletions
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue