aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-01-31 18:17:58 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-03-11 15:41:41 +0100
commit9bc6b85e12d73a43b8d85ba109acff8778f4c08a (patch)
tree48295a08b01988a8aa12674162385681dd937824
parent03f444905d3a7af3091c2401280e83146f08443a (diff)
downloadlibnitrokey-9bc6b85e12d73a43b8d85ba109acff8778f4c08a.tar.gz
libnitrokey-9bc6b85e12d73a43b8d85ba109acff8778f4c08a.tar.bz2
Throw before communicating with device if it is not initialized
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--device.cc13
-rw-r--r--include/LibraryException.h2
-rw-r--r--include/device_proto.h7
3 files changed, 15 insertions, 7 deletions
diff --git a/device.cc b/device.cc
index 42c6883..ce1eeb6 100644
--- a/device.cc
+++ b/device.cc
@@ -7,6 +7,7 @@
#include "include/device.h"
#include "include/log.h"
#include <mutex>
+#include "DeviceCommunicationExceptions.h"
std::mutex mex_dev_com;
@@ -60,8 +61,10 @@ int Device::send(const void *packet) {
std::lock_guard<std::mutex> lock(mex_dev_com);
Log::instance()(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2);
- if (mp_devhandle == nullptr)
- throw std::runtime_error("Attempted HID send on an invalid descriptor."); //TODO migrate except to library_error
+ if (mp_devhandle == nullptr) {
+ Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2);
+ throw DeviceNotConnected("Attempted HID send on an invalid descriptor.");
+ }
return (hid_send_feature_report(
mp_devhandle, (const unsigned char *)(packet), HID_REPORT_SIZE));
@@ -75,8 +78,10 @@ int Device::recv(void *packet) {
int retry_count = 0;
- if (mp_devhandle == nullptr)
- throw std::runtime_error("Attempted HID receive on an invalid descriptor."); //TODO migrate except to library_error
+ if (mp_devhandle == nullptr){
+ Log::instance()(std::string("Connection fail") , Loglevel::DEBUG_L2);
+ throw DeviceNotConnected("Attempted HID receive on an invalid descriptor.");
+ }
// FIXME extract error handling and repeating to parent function in
// device_proto:192
diff --git a/include/LibraryException.h b/include/LibraryException.h
index 3c3fab4..e62788d 100644
--- a/include/LibraryException.h
+++ b/include/LibraryException.h
@@ -11,8 +11,6 @@ public:
virtual uint8_t exception_id()= 0;
};
-
-
class TargetBufferSmallerThanSource: public LibraryException {
public:
virtual uint8_t exception_id() override {
diff --git a/include/device_proto.h b/include/device_proto.h
index ba314f4..9401428 100644
--- a/include/device_proto.h
+++ b/include/device_proto.h
@@ -33,6 +33,7 @@
#define PWS_SEND_CR 3
#include <mutex>
+#include "DeviceCommunicationExceptions.h"
namespace nitrokey {
namespace proto {
@@ -217,6 +218,10 @@ namespace nitrokey {
Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2);
+ if (dev == nullptr){
+ throw DeviceNotConnected("Device not initialized");
+ }
+
int status;
OutgoingPacket outp;
ResponsePacket resp;
@@ -320,7 +325,7 @@ namespace nitrokey {
clear_packet(outp);
if (status <= 0)
- throw std::runtime_error( //FIXME replace with CriticalErrorException
+ throw DeviceReceivingFailure( //FIXME replace with CriticalErrorException
std::string("Device error while executing command ") +
std::to_string(status));