aboutsummaryrefslogtreecommitdiff
path: root/device.cc
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-01-11 16:10:30 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-03-11 15:41:36 +0100
commit2dec896b3f853d2e509c4eb15fe5dd6c28e0a1d7 (patch)
tree5a04ab0aa61e63f381d04a10619b46a3a065a724 /device.cc
parent774f3a0d93bac42aa50d4d30753e00f769b17881 (diff)
downloadlibnitrokey-2dec896b3f853d2e509c4eb15fe5dd6c28e0a1d7.tar.gz
libnitrokey-2dec896b3f853d2e509c4eb15fe5dd6c28e0a1d7.tar.bz2
Use lock_guard while accessing hid_api
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'device.cc')
-rw-r--r--device.cc7
1 files changed, 7 insertions, 0 deletions
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 <mutex>
+
+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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(mex_dev_com);
int status;
int retry_count = 0;