diff options
Diffstat (limited to 'device.cc')
-rw-r--r-- | device.cc | 80 |
1 files changed, 61 insertions, 19 deletions
@@ -45,14 +45,33 @@ const uint16_t nitrokey::device::NITROKEY_VID = 0x20a0; const uint16_t nitrokey::device::NITROKEY_PRO_PID = 0x4108; const uint16_t nitrokey::device::NITROKEY_STORAGE_PID = 0x4109; +const uint16_t nitrokey::device::PURISM_VID = 0x316d; +const uint16_t nitrokey::device::LIBREM_KEY_PID = 0x4c4b; + Option<DeviceModel> nitrokey::device::product_id_to_model(uint16_t product_id) { - switch (product_id) { + return product_id_to_model(NITROKEY_VID, product_id); +} + +Option<DeviceModel> nitrokey::device::product_id_to_model(uint16_t vendor_id, uint16_t product_id) { + switch (vendor_id) { + case NITROKEY_VID: + switch (product_id) { case NITROKEY_PRO_PID: return DeviceModel::PRO; case NITROKEY_STORAGE_PID: return DeviceModel::STORAGE; default: return {}; + } + case PURISM_VID: + switch (product_id) { + case LIBREM_KEY_PID: + return DeviceModel::LIBREM; + default: + return {}; + } + default: + return {}; } } @@ -67,6 +86,9 @@ std::ostream& nitrokey::device::operator<<(std::ostream& stream, DeviceModel mod case DeviceModel::STORAGE: stream << "Storage"; break; + case DeviceModel::LIBREM: + stream << "Librem"; + break; default: stream << "Unknown"; break; @@ -99,7 +121,9 @@ bool Device::disconnect() { } bool Device::_disconnect() { - LOG(std::string(__FUNCTION__) + std::string(m_model == DeviceModel::PRO ? "PRO" : "STORAGE"), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + + std::string(m_model == DeviceModel::PRO ? "PRO" : (m_model == DeviceModel::STORAGE ? "STORAGE" : "LIBREM")), + Loglevel::DEBUG_L2); LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) { @@ -204,27 +228,36 @@ int Device::recv(void *packet) { return status; } -std::vector<DeviceInfo> Device::enumerate(){ - auto pInfo = hid_enumerate(NITROKEY_VID, 0); - auto pInfo_ = pInfo; - std::vector<DeviceInfo> res; - while (pInfo != nullptr){ - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); +namespace { + void add_vendor_devices(std::vector<DeviceInfo>& res, uint16_t vendor_id){ + auto pInfo = hid_enumerate(vendor_id, 0); + auto pInfo_ = pInfo; + while (pInfo != nullptr){ + if (pInfo->path == nullptr || pInfo->serial_number == nullptr) { + continue; + } + auto deviceModel = product_id_to_model(vendor_id, pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = { deviceModel.value(), path, serialNumber }; + res.push_back(info); + } + pInfo = pInfo->next; } - pInfo = pInfo->next; - } - if (pInfo_ != nullptr){ - hid_free_enumeration(pInfo_); + if (pInfo_ != nullptr){ + hid_free_enumeration(pInfo_); + } } +} +std::vector<DeviceInfo> Device::enumerate(){ + std::vector<DeviceInfo> res; + ::add_vendor_devices(res, NITROKEY_VID); + ::add_vendor_devices(res, PURISM_VID); return res; } @@ -234,6 +267,8 @@ std::shared_ptr<Device> Device::create(DeviceModel model) { return std::make_shared<Stick10>(); case DeviceModel::STORAGE: return std::make_shared<Stick20>(); + case DeviceModel::LIBREM: + return std::make_shared<LibremKey>(); default: return {}; } @@ -305,6 +340,13 @@ Stick20::Stick20(): setDefaultDelay(); } + +LibremKey::LibremKey(): + Device(PURISM_VID, LIBREM_KEY_PID, DeviceModel::LIBREM, 100ms, 5, 100ms) + { + setDefaultDelay(); + } + #include <sstream> #define p(x) ss << #x << " " << x << ", "; std::string Device::ErrorCounters::get_as_string() { |