aboutsummaryrefslogtreecommitdiff
path: root/device.cc
diff options
context:
space:
mode:
Diffstat (limited to 'device.cc')
-rw-r--r--device.cc19
1 files changed, 17 insertions, 2 deletions
diff --git a/device.cc b/device.cc
index c954cb7..e1a5615 100644
--- a/device.cc
+++ b/device.cc
@@ -1,10 +1,14 @@
#include <chrono>
#include <thread>
#include <cstddef>
+#include <stdexcept>
#include <hidapi/hidapi.h>
#include "device.h"
+#include "log.h"
+#include "misc.h"
-using namespace device;
+using namespace nitrokey::device;
+using namespace nitrokey::log;
Device::Device()
: m_vid(0), m_pid(0),
@@ -13,13 +17,19 @@ Device::Device()
mp_devhandle(NULL) {}
bool Device::connect() {
- hid_init();
+ Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
+ hid_init();
mp_devhandle = hid_open(m_vid, m_pid, NULL);
return mp_devhandle != NULL;
}
CommError Device::send(const void *packet) {
+ Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
+
+ if (mp_devhandle == NULL)
+ throw std::runtime_error("Attempted HID send on an invalid descriptor.");
+
return (CommError)(
hid_send_feature_report(mp_devhandle, (const unsigned char *)(packet),
HID_REPORT_SIZE));
@@ -29,6 +39,11 @@ CommError Device::recv(void *packet) {
CommError status;
int retry_count = 0;
+ Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
+
+ if (mp_devhandle == NULL)
+ throw std::runtime_error("Attempted HID receive on an invalid descriptor.");
+
for(;;) {
status = (CommError)(
hid_get_feature_report(mp_devhandle, (unsigned char *)(packet),