Skip to content

Commit

Permalink
Throw before communicating with device if it is not initialized
Browse files Browse the repository at this point in the history
Signed-off-by: Szczepan Zalega <[email protected]>
  • Loading branch information
szszszsz committed Mar 11, 2017
1 parent 03f4449 commit 9bc6b85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
13 changes: 9 additions & 4 deletions device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "include/device.h"
#include "include/log.h"
#include <mutex>
#include "DeviceCommunicationExceptions.h"

std::mutex mex_dev_com;

Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions include/LibraryException.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class LibraryException: std::exception {
virtual uint8_t exception_id()= 0;
};



class TargetBufferSmallerThanSource: public LibraryException {
public:
virtual uint8_t exception_id() override {
Expand Down
7 changes: 6 additions & 1 deletion include/device_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define PWS_SEND_CR 3

#include <mutex>
#include "DeviceCommunicationExceptions.h"

namespace nitrokey {
namespace proto {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit 9bc6b85

Please sign in to comment.