From 4e1642ca1e633d4fcec3045155d0f13c26b0f0b5 Mon Sep 17 00:00:00 2001 From: Ondrej Mikle Date: Sat, 19 Jul 2014 02:23:57 +0200 Subject: [PATCH] Interface and device initialization. --- brmdoor_nfc.cpp | 28 ++++++++++++++++++++++++++++ brmdoor_nfc.h | 26 +++++++++++++++++++++++++- brmdoor_nfc.i | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/brmdoor_nfc.cpp b/brmdoor_nfc.cpp index 65fd701..8f48fb9 100644 --- a/brmdoor_nfc.cpp +++ b/brmdoor_nfc.cpp @@ -12,6 +12,30 @@ NFCDevice::NFCDevice() { pollNr = 20; pollPeriod = 2; + + _nfcContext = NULL; + + nfc_init(&_nfcContext); + if (_nfcContext == NULL) { + throw NFCError("Unable to init libnfc (malloc)"); + } + + _nfcDevice = nfc_open(_nfcContext, NULL); + + if (_nfcDevice == NULL) { + throw NFCError("Unable to open NFC device."); + } + + if (nfc_initiator_init(_nfcDevice) < 0) { + nfc_close(_nfcDevice); + throw NFCError("NFC initiator error"); + } + +} + +NFCDevice::~NFCDevice() +{ + nfc_exit(_nfcContext); } std::string NFCDevice::scanUID() @@ -30,3 +54,7 @@ const nfc_modulation NFCDevice::_modulations[5] = { const size_t _modulationsLen = 5; +NFCError::NFCError(const std::string& msg) +{ + _msg = msg; +} diff --git a/brmdoor_nfc.h b/brmdoor_nfc.h index db5698b..add9c06 100644 --- a/brmdoor_nfc.h +++ b/brmdoor_nfc.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -13,7 +14,7 @@ public: NFCDevice(); - ~NFCDevice() {} + virtual ~NFCDevice(); std::string scanUID(); @@ -24,6 +25,29 @@ public: protected: static const nfc_modulation _modulations[5]; + static const size_t _modulationsLen = 5; + nfc_context *_nfcContext; + + nfc_device *_nfcDevice; + }; + + +class NFCError: public std::exception +{ + +public: + + NFCError(const std::string& msg); + + const char *what() const throw() {return _msg.c_str();} + + ~NFCError() throw() {} + +protected: + + std::string _msg; +}; + diff --git a/brmdoor_nfc.i b/brmdoor_nfc.i index f89e475..98b8c4a 100644 --- a/brmdoor_nfc.i +++ b/brmdoor_nfc.i @@ -6,6 +6,7 @@ %} %include std_string.i +%include std_except.i %include "brmdoor_nfc.h"