From 2dec896b3f853d2e509c4eb15fe5dd6c28e0a1d7 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:10:30 +0100 Subject: Use lock_guard while accessing hid_api Signed-off-by: Szczepan Zalega --- device.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/device.cc b/device.cc index 6eaaca5..7bdbe9a 100644 --- a/device.cc +++ b/device.cc @@ -6,6 +6,9 @@ #include "include/misc.h" #include "include/device.h" #include "include/log.h" +#include + +std::mutex mex_dev_com; using namespace nitrokey::device; using namespace nitrokey::log; @@ -26,6 +29,7 @@ Device::Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, {} bool Device::disconnect() { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) return false; @@ -35,6 +39,7 @@ bool Device::disconnect() { return true; } bool Device::connect() { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); // hid_init(); // done automatically on hid_open @@ -43,6 +48,7 @@ bool Device::connect() { } int Device::send(const void *packet) { + std::lock_guard lock(mex_dev_com); Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); if (mp_devhandle == NULL) @@ -53,6 +59,7 @@ int Device::send(const void *packet) { } int Device::recv(void *packet) { + std::lock_guard lock(mex_dev_com); int status; int retry_count = 0; -- cgit v1.2.3