diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2017-12-04 16:29:56 +0100 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2018-02-28 19:23:04 +0100 |
commit | 2faa8f6782a2e6294ed8849048a281d12d60da1c (patch) | |
tree | 0e8947f40697da905278c44338f948ce5fa2b3c7 /device.cc | |
parent | d5486ba77235a874245fbee07a75cea89fa59ea2 (diff) | |
download | libnitrokey-2faa8f6782a2e6294ed8849048a281d12d60da1c.tar.gz libnitrokey-2faa8f6782a2e6294ed8849048a281d12d60da1c.tar.bz2 |
Initial support for multiple devices with C++ and test
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'device.cc')
-rw-r--r-- | device.cc | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -20,6 +20,7 @@ */ #include <chrono> +#include <iostream> #include <thread> #include <cstddef> #include <stdexcept> @@ -92,12 +93,20 @@ bool Device::_connect() { LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open - mp_devhandle = hid_open(m_vid, m_pid, nullptr); + if (m_path.empty()){ + mp_devhandle = hid_open(m_vid, m_pid, nullptr); + } else { + mp_devhandle = hid_open_path(m_path.c_str()); + } const bool success = mp_devhandle != nullptr; LOG(std::string("Connection success: ") + std::to_string(success), Loglevel::DEBUG_L2); return success; } +void Device::set_path(const std::string path){ + m_path = path; +} + int Device::send(const void *packet) { LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard<std::mutex> lock(mex_dev_com); @@ -160,6 +169,24 @@ int Device::recv(void *packet) { return status; } +std::vector<std::string> Device::enumerate(){ + //TODO make static + auto pInfo = hid_enumerate(m_vid, m_pid); + auto pInfo_ = pInfo; + std::vector<std::string> res; + while (pInfo != nullptr){ + std::string a (pInfo->path); + res.push_back(a); + pInfo = pInfo->next; + } + + if (pInfo_ != nullptr){ + hid_free_enumeration(pInfo_); + } + + return res; +} + bool Device::could_be_enumerated() { LOG(__FUNCTION__, Loglevel::DEBUG_L2); std::lock_guard<std::mutex> lock(mex_dev_com); |