aboutsummaryrefslogtreecommitdiff
path: root/device.cc
diff options
context:
space:
mode:
Diffstat (limited to 'device.cc')
-rw-r--r--device.cc97
1 files changed, 69 insertions, 28 deletions
diff --git a/device.cc b/device.cc
index c954cb7..08cba49 100644
--- a/device.cc
+++ b/device.cc
@@ -1,52 +1,93 @@
#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),
- m_retry_count(4),
- m_retry_timeout(50),
- mp_devhandle(NULL) {}
+ : m_vid(0),
+ m_pid(0),
+ m_retry_count(40),
+ m_retry_timeout(50),
+ mp_devhandle(NULL) {}
+bool Device::disconnect() {
+ Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
+
+ hid_exit();
+ mp_devhandle = NULL;
+ return true;
+}
bool Device::connect() {
- hid_init();
+ Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
- mp_devhandle = hid_open(m_vid, m_pid, NULL);
- return mp_devhandle != NULL;
+ // hid_init();
+ mp_devhandle = hid_open(m_vid, m_pid, NULL);
+ // hid_init();
+ return mp_devhandle != NULL;
}
CommError Device::send(const void *packet) {
- return (CommError)(
- hid_send_feature_report(mp_devhandle, (const unsigned char *)(packet),
- HID_REPORT_SIZE));
+ 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));
}
CommError Device::recv(void *packet) {
- CommError status;
- int retry_count = 0;
-
- for(;;) {
- status = (CommError)(
- hid_get_feature_report(mp_devhandle, (unsigned char *)(packet),
- HID_REPORT_SIZE));
- if (status == CommError::ERR_NO_ERROR || retry_count++ >= m_retry_count)
- break;
- std::this_thread::sleep_for(m_retry_timeout);
- }
-
- return status;
+ // FIXME change CommError return value to int (return value of
+ // hid_get_feature_report)
+ int 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.");
+
+ // FIXME extract error handling and repeating to parent function in
+ // device_proto:192
+ for (;;) {
+ status = (hid_get_feature_report(mp_devhandle, (unsigned char *)(packet),
+ HID_REPORT_SIZE));
+
+ // FIXME handle getting libhid error message somewhere else
+ auto pwherr = hid_error(mp_devhandle);
+ std::wstring wherr = (pwherr != NULL) ? pwherr : L"No error message";
+ std::string herr(wherr.begin(), wherr.end());
+ Log::instance()(std::string("libhid error message: ") + herr,
+ Loglevel::DEBUG_L2);
+
+ if (status > 0) break; // success
+ if (retry_count++ >= m_retry_count) {
+ Log::instance()(
+ "Maximum retry count reached" + std::to_string(retry_count),
+ Loglevel::WARNING);
+ break;
+ }
+ Log::instance()("Retrying... " + std::to_string(retry_count),
+ Loglevel::DEBUG);
+ std::this_thread::sleep_for(m_retry_timeout);
+ }
+
+ return (CommError)status;
}
Stick10::Stick10() {
- m_vid = 0x20a0;
- m_pid = 0x4108;
+ m_vid = 0x20a0;
+ m_pid = 0x4108;
}
Stick20::Stick20() {
- m_vid = 0x20a0;
- m_pid = 0x4109;
+ m_vid = 0x20a0;
+ m_pid = 0x4109;
}