From b70c1e855a25abdf4c9e25d9c5275ac9866b4e2d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 14 Oct 2016 13:31:35 +0200 Subject: Code reformat Signed-off-by: Szczepan Zalega --- include/device_proto.h | 423 +++++++++++++++++++++++++------------------------ 1 file changed, 214 insertions(+), 209 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 4044cdc..ebe31df 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -1,5 +1,6 @@ #ifndef DEVICE_PROTO_H #define DEVICE_PROTO_H + #include #include #include @@ -31,7 +32,7 @@ #define PWS_SEND_CR 3 namespace nitrokey { -namespace proto { + namespace proto { /* * POD types for HID proto commands * Instances are meant to be __packed. @@ -42,45 +43,45 @@ namespace proto { /* * Every packet is a USB HID report (check USB spec) */ -template -struct HIDReport { - uint8_t _zero; - CommandID command_id; // uint8_t - union { - uint8_t _padding[HID_REPORT_SIZE - 6]; - Payload payload; - } __packed; - uint32_t crc; - - // POD types can't have non-default constructors - // used in Transaction<>::run() - void initialize() { - bzero(this, sizeof *this); - command_id = cmd_id; - } - - uint32_t calculate_CRC() const { - // w/o leading zero, a part of each HID packet - // w/o 4-byte crc - return misc::stm_crc32((const uint8_t *)(this) + 1, - (size_t)(HID_REPORT_SIZE - 5)); - } - - void update_CRC() { crc = calculate_CRC(); } - - bool isCRCcorrect() const { return crc == calculate_CRC(); } - - bool isValid() const { - return true; - // return !_zero && payload.isValid() && isCRCcorrect(); - } - - operator std::string() const { - // Packet type is known upfront in normal operation. - // Can't be used to dissect random packets. - return QueryDissector::dissect(*this); - } -} __packed; + template + struct HIDReport { + uint8_t _zero; + CommandID command_id; // uint8_t + union { + uint8_t _padding[HID_REPORT_SIZE - 6]; + Payload payload; + } __packed; + uint32_t crc; + + // POD types can't have non-default constructors + // used in Transaction<>::run() + void initialize() { + bzero(this, sizeof *this); + command_id = cmd_id; + } + + uint32_t calculate_CRC() const { + // w/o leading zero, a part of each HID packet + // w/o 4-byte crc + return misc::stm_crc32((const uint8_t *) (this) + 1, + (size_t) (HID_REPORT_SIZE - 5)); + } + + void update_CRC() { crc = calculate_CRC(); } + + bool isCRCcorrect() const { return crc == calculate_CRC(); } + + bool isValid() const { + return true; + // return !_zero && payload.isValid() && isCRCcorrect(); + } + + operator std::string() const { + // Packet type is known upfront in normal operation. + // Can't be used to dissect random packets. + return QueryDissector::dissect(*this); + } + } __packed; /* * Response payload (the parametrized type inside struct HIDReport) @@ -88,175 +89,179 @@ struct HIDReport { * command_id member in incoming HIDReport structure carries the command * type last used. */ -template -struct DeviceResponse { - uint8_t _zero; - uint8_t device_status; - uint8_t command_id; // originally last_command_type - uint32_t last_command_crc; - uint8_t last_command_status; - union { - uint8_t _padding[HID_REPORT_SIZE - 12]; - ResponsePayload payload; - } __packed; - uint32_t crc; - - void initialize() { bzero(this, sizeof *this); } - - uint32_t calculate_CRC() const { - // w/o leading zero, a part of each HID packet - // w/o 4-byte crc - return misc::stm_crc32((const uint8_t *)(this) + 1, - (size_t)(HID_REPORT_SIZE - 5)); - } - - void update_CRC() { crc = calculate_CRC(); } - - bool isCRCcorrect() const { return crc == calculate_CRC(); } - - bool isValid() const { - // return !_zero && payload.isValid() && isCRCcorrect() && - // command_id == (uint8_t)(cmd_id); - return true; - } - - operator std::string() const { - return ResponseDissector::dissect(*this); - } -} __packed; - -struct EmptyPayload { - uint8_t _data[]; - - bool isValid() const { return true; } - - std::string dissect() const { return std::string("Empty Payload."); } -} __packed; - -template -class ClearingProxy{ -public: - ClearingProxy(command_packet &p){ - packet = p; - bzero(&p, sizeof(p)); - } - ~ClearingProxy(){ - bzero(&packet, sizeof(packet)); - } - - response_payload & data() { - return packet.payload; + template + struct DeviceResponse { + uint8_t _zero; + uint8_t device_status; + uint8_t command_id; // originally last_command_type + uint32_t last_command_crc; + uint8_t last_command_status; + union { + uint8_t _padding[HID_REPORT_SIZE - 12]; + ResponsePayload payload; + } __packed; + uint32_t crc; + + void initialize() { bzero(this, sizeof *this); } + + uint32_t calculate_CRC() const { + // w/o leading zero, a part of each HID packet + // w/o 4-byte crc + return misc::stm_crc32((const uint8_t *) (this) + 1, + (size_t) (HID_REPORT_SIZE - 5)); + } + + void update_CRC() { crc = calculate_CRC(); } + + bool isCRCcorrect() const { return crc == calculate_CRC(); } + + bool isValid() const { + // return !_zero && payload.isValid() && isCRCcorrect() && + // command_id == (uint8_t)(cmd_id); + return true; + } + + operator std::string() const { + return ResponseDissector::dissect(*this); + } + } __packed; + + struct EmptyPayload { + uint8_t _data[]; + + bool isValid() const { return true; } + + std::string dissect() const { return std::string("Empty Payload."); } + } __packed; + + template + class ClearingProxy { + public: + ClearingProxy(command_packet &p) { + packet = p; + bzero(&p, sizeof(p)); + } + + ~ClearingProxy() { + bzero(&packet, sizeof(packet)); + } + + response_payload &data() { + return packet.payload; + } + + command_packet packet; + }; + + template + class Transaction : semantics::non_constructible { + public: + // Types declared in command class scope can't be reached from there. + typedef command_payload CommandPayload; + typedef response_payload ResponsePayload; + + typedef struct HIDReport OutgoingPacket; + typedef struct DeviceResponse ResponsePacket; + + static_assert(std::is_pod::value, + "outgoingpacket must be a pod type"); + static_assert(std::is_pod::value, + "ResponsePacket must be a POD type"); + static_assert(sizeof(OutgoingPacket) == HID_REPORT_SIZE, + "OutgoingPacket type is not the right size"); + static_assert(sizeof(ResponsePacket) == HID_REPORT_SIZE, + "ResponsePacket type is not the right size"); + + static uint32_t getCRC( + const command_payload &payload) { + OutgoingPacket outp; + outp.initialize(); + outp.payload = payload; + outp.update_CRC(); + return outp.crc; + } + + template + static void clear_packet(T &st) { + bzero(&st, sizeof(st)); + } + + + static ClearingProxy run(device::Device &dev, + const command_payload &payload) { + using namespace ::nitrokey::device; + using namespace ::nitrokey::log; + using namespace std::chrono_literals; + + Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + + int status; + OutgoingPacket outp; + ResponsePacket resp; + + // POD types can't have non-default constructors + outp.initialize(); + resp.initialize(); + + outp.payload = payload; + outp.update_CRC(); + + Log::instance()("Outgoing HID packet:", Loglevel::DEBUG); + Log::instance()((std::string) (outp), Loglevel::DEBUG); + + if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); + + status = dev.send(&outp); + if (status <= 0) + throw std::runtime_error( + std::string("Device error while sending command ") + + std::to_string((int) (status))); + + std::this_thread::sleep_for(dev.get_send_receive_delay()); + + // FIXME make checks done in device:recv here + int retry = dev.get_retry_count(); + while (retry-- > 0) { + status = dev.recv(&resp); + + dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv + + if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; + Log::instance()( + "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", + Loglevel::DEBUG); + Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); + Log::instance()((std::string) (resp), Loglevel::DEBUG_L2); + std::this_thread::sleep_for(dev.get_retry_timeout()); + continue; + } + clear_packet(outp); + + if (status <= 0) + throw std::runtime_error( + std::string("Device error while executing command ") + + std::to_string(status)); + + Log::instance()("Incoming HID packet:", Loglevel::DEBUG); + Log::instance()((std::string) (resp), Loglevel::DEBUG); + Log::instance()(std::string("Retry count: ") + std::to_string(retry), Loglevel::DEBUG); + + if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); + if (retry <= 0) + throw std::runtime_error("Maximum retry count reached for receiving response from the device!"); + if (resp.last_command_status != 0) + throw CommandFailedException(resp.command_id, resp.last_command_status); + + + // See: DeviceResponse + return resp; + } + + static ClearingProxy run(device::Device &dev) { + command_payload empty_payload; + return run(dev, empty_payload); + } + }; } - - command_packet packet; -}; - -template -class Transaction : semantics::non_constructible { - public: - // Types declared in command class scope can't be reached from there. - typedef command_payload CommandPayload; - typedef response_payload ResponsePayload; - - typedef struct HIDReport OutgoingPacket; - typedef struct DeviceResponse ResponsePacket; - - static_assert(std::is_pod::value, - "outgoingpacket must be a pod type"); - static_assert(std::is_pod::value, - "ResponsePacket must be a POD type"); - static_assert(sizeof(OutgoingPacket) == HID_REPORT_SIZE, - "OutgoingPacket type is not the right size"); - static_assert(sizeof(ResponsePacket) == HID_REPORT_SIZE, - "ResponsePacket type is not the right size"); - - static uint32_t getCRC( - const command_payload &payload) { - OutgoingPacket outp; - outp.initialize(); - outp.payload = payload; - outp.update_CRC(); - return outp.crc; - } - - template - static void clear_packet(T &st){ - bzero(&st, sizeof(st)); - } - - - static ClearingProxy run(device::Device &dev, - const command_payload &payload) { - using namespace ::nitrokey::device; - using namespace ::nitrokey::log; - using namespace std::chrono_literals; - - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); - - int status; - OutgoingPacket outp; - ResponsePacket resp; - - // POD types can't have non-default constructors - outp.initialize(); - resp.initialize(); - - outp.payload = payload; - outp.update_CRC(); - - Log::instance()("Outgoing HID packet:", Loglevel::DEBUG); - Log::instance()((std::string)(outp), Loglevel::DEBUG); - - if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); - - status = dev.send(&outp); - if (status <= 0) - throw std::runtime_error( - std::string("Device error while sending command ") + - std::to_string((int)(status))); - - std::this_thread::sleep_for(dev.get_send_receive_delay()); - - // FIXME make checks done in device:recv here - int retry = dev.get_retry_count(); - while (retry-- > 0) { - status = dev.recv(&resp); - - dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv - - if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; - Log::instance()("Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", - Loglevel::DEBUG); - Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); - Log::instance()((std::string)(resp), Loglevel::DEBUG_L2); - std::this_thread::sleep_for(dev.get_retry_timeout()); - continue; - } - clear_packet(outp); - - if (status <= 0) - throw std::runtime_error( - std::string("Device error while executing command ") + - std::to_string(status)); - - Log::instance()("Incoming HID packet:", Loglevel::DEBUG); - Log::instance()((std::string)(resp), Loglevel::DEBUG); - Log::instance()(std::string("Retry count: ") + std::to_string(retry), Loglevel::DEBUG); - - if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); - if (retry <= 0) throw std::runtime_error("Maximum retry count reached for receiving response from the device!"); - if (resp.last_command_status!=0) throw CommandFailedException(resp.command_id, resp.last_command_status); - - - // See: DeviceResponse - return resp; - } - - static ClearingProxy run(device::Device &dev) { - command_payload empty_payload; - return run(dev, empty_payload); - } -}; -} } #endif -- cgit v1.2.1 From a8965c05ff8b69d6f0dc51e41655acf8b198869b Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 14 Oct 2016 13:35:13 +0200 Subject: Handle NK Storage status sent on Storage commands Signed-off-by: Szczepan Zalega --- include/device_proto.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index ebe31df..6e99eaa 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -99,6 +99,13 @@ namespace nitrokey { union { uint8_t _padding[HID_REPORT_SIZE - 12]; ResponsePayload payload; + struct{ + uint8_t _storageStatusPadding[20-8+1]; //starts on 20th byte minus already 8 used + zero byte + uint8_t CommandCounter_u8; + uint8_t LastCommand_u8; + uint8_t Status_u8; //general status - idle0/ok1/busy2/wrongpassword3 + uint8_t ProgressBarValue_u8; + } StorageStatus __packed; } __packed; uint32_t crc; @@ -224,9 +231,40 @@ namespace nitrokey { while (retry-- > 0) { status = dev.recv(&resp); + if (dev.get_device_model() == DeviceModel::STORAGE && + resp.command_id >= 0x20 && +// resp.command_id <= 0x20 + 26 + resp.command_id < 0x60 + ){ + Log::instance()(std::string("Detected storage device cmd, status: ") + + std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG_L2); + + resp.last_command_status = 0; + switch(resp.StorageStatus.Status_u8){ + case 0: + case 1: + resp.last_command_status = 0; + resp.device_status = 0; + break; + case 2: + resp.last_command_status = 0; + resp.device_status = 1; //pro busy + break; + case 3: + case 4: + resp.last_command_status = 4; + resp.device_status = 0; + break; + }; + } + dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; + Log::instance()(std::string("Retry status: ") + + std::to_string(resp.device_status) + " " + + std::to_string(resp.last_command_crc==outp.crc), Loglevel::DEBUG_L2); + Log::instance()( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); -- cgit v1.2.1 From 62f48fa92db1be555a955328c6654a301cf6f7c8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 17 Oct 2016 22:04:20 +0200 Subject: Make packet valid when its crc is not equal 0 Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 6e99eaa..ff8bb67 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -125,7 +125,7 @@ namespace nitrokey { bool isValid() const { // return !_zero && payload.isValid() && isCRCcorrect() && // command_id == (uint8_t)(cmd_id); - return true; + return crc != 0; } operator std::string() const { -- cgit v1.2.1 From a47a7bc3c838a37e15e4464be75736cd83573da2 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 17 Oct 2016 22:08:59 +0200 Subject: Resend outgoing packet on invalid crc and when used all receiving retries Signed-off-by: Szczepan Zalega --- include/device_proto.h | 117 ++++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 51 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index ff8bb67..2d39a04 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -218,61 +218,76 @@ namespace nitrokey { if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); - status = dev.send(&outp); - if (status <= 0) - throw std::runtime_error( - std::string("Device error while sending command ") + - std::to_string((int) (status))); - - std::this_thread::sleep_for(dev.get_send_receive_delay()); - - // FIXME make checks done in device:recv here - int retry = dev.get_retry_count(); - while (retry-- > 0) { - status = dev.recv(&resp); - - if (dev.get_device_model() == DeviceModel::STORAGE && - resp.command_id >= 0x20 && -// resp.command_id <= 0x20 + 26 - resp.command_id < 0x60 - ){ + int retry = 0; + int sending_retry_counter = 3; + while (sending_retry_counter-->0) { + status = dev.send(&outp); + if (status <= 0) + throw std::runtime_error( + std::string("Device error while sending command ") + + std::to_string((int) (status))); + + std::this_thread::sleep_for(dev.get_send_receive_delay()); + + // FIXME make checks done in device:recv here + retry = dev.get_retry_count(); + while (retry-- > 0) { + status = dev.recv(&resp); + + if (dev.get_device_model() == DeviceModel::STORAGE && + resp.command_id >= 0x20 && + // resp.command_id <= 0x20 + 26 + resp.command_id < 0x60 + ) { Log::instance()(std::string("Detected storage device cmd, status: ") + - std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG_L2); - - resp.last_command_status = 0; - switch(resp.StorageStatus.Status_u8){ - case 0: - case 1: - resp.last_command_status = 0; - resp.device_status = 0; - break; - case 2: - resp.last_command_status = 0; - resp.device_status = 1; //pro busy - break; - case 3: - case 4: - resp.last_command_status = 4; - resp.device_status = 0; - break; - }; + std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG_L2); + + resp.last_command_status = 0; + switch (resp.StorageStatus.Status_u8) { + case 0: + case 1: + resp.last_command_status = 0; + resp.device_status = 0; + break; + case 2: + resp.last_command_status = 0; + resp.device_status = 1; //pro busy + break; + case 3: + case 4: + resp.last_command_status = 4; + resp.device_status = 0; + break; + }; + } + + //SENDPASSWORD gives wrong CRC , for now rely on !=0 (TODO report) +// if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; + if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isValid()) break; + if (resp.device_status == 1 ) { + retry++; + Log::instance()("Status busy, not decresing retry counter: " + std::to_string(retry), Loglevel::DEBUG_L2); + } + Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + + std::to_string(resp.device_status) + " " + + std::to_string(resp.last_command_crc == outp.crc) + + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); + + Log::instance()( + "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", + Loglevel::DEBUG); + Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); + Log::instance()((std::string) (resp), Loglevel::DEBUG_L2); + std::this_thread::sleep_for(dev.get_retry_timeout()); + continue; } - - dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv - if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; - Log::instance()(std::string("Retry status: ") - + std::to_string(resp.device_status) + " " + - std::to_string(resp.last_command_crc==outp.crc), Loglevel::DEBUG_L2); - - Log::instance()( - "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", - Loglevel::DEBUG); - Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); - Log::instance()((std::string) (resp), Loglevel::DEBUG_L2); - std::this_thread::sleep_for(dev.get_retry_timeout()); - continue; + Log::instance()(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); + Log::instance()(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), Loglevel::DEBUG); } + + dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv + clear_packet(outp); if (status <= 0) -- cgit v1.2.1 From 803a6aa41e82828dcbe7e02bcdcf20d776b60050 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 17 Oct 2016 22:10:18 +0200 Subject: Code refactoring: rename - disambiguate receiving counter from sending Signed-off-by: Szczepan Zalega --- include/device_proto.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 2d39a04..9f0d1e9 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -218,7 +218,7 @@ namespace nitrokey { if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); - int retry = 0; + int receiving_retry_counter = 0; int sending_retry_counter = 3; while (sending_retry_counter-->0) { status = dev.send(&outp); @@ -230,8 +230,8 @@ namespace nitrokey { std::this_thread::sleep_for(dev.get_send_receive_delay()); // FIXME make checks done in device:recv here - retry = dev.get_retry_count(); - while (retry-- > 0) { + receiving_retry_counter = dev.get_retry_count(); + while (receiving_retry_counter-- > 0) { status = dev.recv(&resp); if (dev.get_device_model() == DeviceModel::STORAGE && @@ -265,8 +265,8 @@ namespace nitrokey { // if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isValid()) break; if (resp.device_status == 1 ) { - retry++; - Log::instance()("Status busy, not decresing retry counter: " + std::to_string(retry), Loglevel::DEBUG_L2); + receiving_retry_counter++; + Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + std::to_string(resp.device_status) + " " + @@ -297,11 +297,11 @@ namespace nitrokey { Log::instance()("Incoming HID packet:", Loglevel::DEBUG); Log::instance()((std::string) (resp), Loglevel::DEBUG); - Log::instance()(std::string("Retry count: ") + std::to_string(retry), Loglevel::DEBUG); + Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), Loglevel::DEBUG); if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); - if (retry <= 0) - throw std::runtime_error("Maximum retry count reached for receiving response from the device!"); + if (receiving_retry_counter <= 0) + throw std::runtime_error("Maximum receiving_retry_counter count reached for receiving response from the device!"); if (resp.last_command_status != 0) throw CommandFailedException(resp.command_id, resp.last_command_status); -- cgit v1.2.1 From 29e1d4322dcef1c7f45d97ffbe3d9887e41eb453 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 11:28:55 +0200 Subject: Code reformat Signed-off-by: Szczepan Zalega --- include/device_proto.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 9f0d1e9..15e3636 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -99,8 +99,8 @@ namespace nitrokey { union { uint8_t _padding[HID_REPORT_SIZE - 12]; ResponsePayload payload; - struct{ - uint8_t _storageStatusPadding[20-8+1]; //starts on 20th byte minus already 8 used + zero byte + struct { + uint8_t _storageStatusPadding[20 - 8 + 1]; //starts on 20th byte minus already 8 used + zero byte uint8_t CommandCounter_u8; uint8_t LastCommand_u8; uint8_t Status_u8; //general status - idle0/ok1/busy2/wrongpassword3 @@ -220,7 +220,7 @@ namespace nitrokey { int receiving_retry_counter = 0; int sending_retry_counter = 3; - while (sending_retry_counter-->0) { + while (sending_retry_counter-- > 0) { status = dev.send(&outp); if (status <= 0) throw std::runtime_error( @@ -236,7 +236,6 @@ namespace nitrokey { if (dev.get_device_model() == DeviceModel::STORAGE && resp.command_id >= 0x20 && - // resp.command_id <= 0x20 + 26 resp.command_id < 0x60 ) { Log::instance()(std::string("Detected storage device cmd, status: ") + @@ -264,14 +263,15 @@ namespace nitrokey { //SENDPASSWORD gives wrong CRC , for now rely on !=0 (TODO report) // if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isValid()) break; - if (resp.device_status == 1 ) { + if (resp.device_status == 1) { receiving_retry_counter++; - Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); + Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(resp.last_command_crc == outp.crc) + - " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); Log::instance()( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", @@ -283,7 +283,8 @@ namespace nitrokey { } if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; Log::instance()(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); - Log::instance()(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), Loglevel::DEBUG); + Log::instance()(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), + Loglevel::DEBUG); } dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv @@ -297,11 +298,13 @@ namespace nitrokey { Log::instance()("Incoming HID packet:", Loglevel::DEBUG); Log::instance()((std::string) (resp), Loglevel::DEBUG); - Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), Loglevel::DEBUG); + Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), + Loglevel::DEBUG); if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); if (receiving_retry_counter <= 0) - throw std::runtime_error("Maximum receiving_retry_counter count reached for receiving response from the device!"); + throw std::runtime_error( + "Maximum receiving_retry_counter count reached for receiving response from the device!"); if (resp.last_command_status != 0) throw CommandFailedException(resp.command_id, resp.last_command_status); -- cgit v1.2.1 From 056228aae33642013053288a932a65c93271b0be Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 12:51:25 +0200 Subject: Code refactoring: c++ casts Signed-off-by: Szczepan Zalega --- include/device_proto.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 15e3636..81cf93e 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -214,7 +214,7 @@ namespace nitrokey { outp.update_CRC(); Log::instance()("Outgoing HID packet:", Loglevel::DEBUG); - Log::instance()((std::string) (outp), Loglevel::DEBUG); + Log::instance()(static_cast(outp), Loglevel::DEBUG); if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); @@ -225,7 +225,7 @@ namespace nitrokey { if (status <= 0) throw std::runtime_error( std::string("Device error while sending command ") + - std::to_string((int) (status))); + std::to_string(status)); std::this_thread::sleep_for(dev.get_send_receive_delay()); @@ -277,7 +277,7 @@ namespace nitrokey { "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); - Log::instance()((std::string) (resp), Loglevel::DEBUG_L2); + Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); std::this_thread::sleep_for(dev.get_retry_timeout()); continue; } @@ -297,7 +297,7 @@ namespace nitrokey { std::to_string(status)); Log::instance()("Incoming HID packet:", Loglevel::DEBUG); - Log::instance()((std::string) (resp), Loglevel::DEBUG); + Log::instance()(static_cast(resp), Loglevel::DEBUG); Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), Loglevel::DEBUG); -- cgit v1.2.1 From 98a9730783268b01d8f55b8b323bb70fdd964a11 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 15:16:39 +0200 Subject: Code refactoring: replacing magic numbers Signed-off-by: Szczepan Zalega --- include/device_proto.h | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 81cf93e..bf78d29 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -235,27 +235,29 @@ namespace nitrokey { status = dev.recv(&resp); if (dev.get_device_model() == DeviceModel::STORAGE && - resp.command_id >= 0x20 && - resp.command_id < 0x60 - ) { + resp.command_id >= stick20::CMD_START_VALUE && + resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG_L2); - resp.last_command_status = 0; - switch (resp.StorageStatus.Status_u8) { - case 0: - case 1: - resp.last_command_status = 0; - resp.device_status = 0; + resp.last_command_status = static_cast(stick10::command_status::ok); + switch (static_cast(resp.StorageStatus.Status_u8)) { + case stick20::device_status::idle : + case stick20::device_status::ok: + resp.device_status = static_cast(stick10::device_status::ok); break; - case 2: - resp.last_command_status = 0; - resp.device_status = 1; //pro busy + case stick20::device_status::busy: + case stick20::device_status::busy_progressbar: //TODO this will be modified later for getting progressbar status + resp.device_status = static_cast(stick10::device_status::busy); break; - case 3: - case 4: - resp.last_command_status = 4; - resp.device_status = 0; + case stick20::device_status::wrong_password: + resp.last_command_status = static_cast(stick10::device_status::wrong_password); + resp.device_status = static_cast(stick10::device_status::ok); + break; + default: + Log::instance()(std::string("Unknown storage device status, cannot translate: ") + + std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG); + resp.device_status = resp.StorageStatus.Status_u8; break; }; } -- cgit v1.2.1 From b33580e849bbdc8162135e74c9bd0405da454883 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 15:19:55 +0200 Subject: Code refactoring: removing disambiguity from device constants Signed-off-by: Szczepan Zalega --- include/device_proto.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index bf78d29..d64c341 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -219,7 +219,7 @@ namespace nitrokey { if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); int receiving_retry_counter = 0; - int sending_retry_counter = 3; + int sending_retry_counter = dev.get_retry_sending_count(); while (sending_retry_counter-- > 0) { status = dev.send(&outp); if (status <= 0) @@ -230,7 +230,7 @@ namespace nitrokey { std::this_thread::sleep_for(dev.get_send_receive_delay()); // FIXME make checks done in device:recv here - receiving_retry_counter = dev.get_retry_count(); + receiving_retry_counter = dev.get_retry_receiving_count(); while (receiving_retry_counter-- > 0) { status = dev.recv(&resp); -- cgit v1.2.1 From f9cc44df7e2882557e37cfb1e0ebe6d779d5eeb5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 15:29:20 +0200 Subject: Code refactoring: variables names unification Signed-off-by: Szczepan Zalega --- include/device_proto.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index d64c341..128eca8 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -100,12 +100,12 @@ namespace nitrokey { uint8_t _padding[HID_REPORT_SIZE - 12]; ResponsePayload payload; struct { - uint8_t _storageStatusPadding[20 - 8 + 1]; //starts on 20th byte minus already 8 used + zero byte - uint8_t CommandCounter_u8; - uint8_t LastCommand_u8; - uint8_t Status_u8; //general status - idle0/ok1/busy2/wrongpassword3 - uint8_t ProgressBarValue_u8; - } StorageStatus __packed; + uint8_t _storage_status_padding[20 - 8 + 1]; //starts on 20th byte minus already 8 used + zero byte + uint8_t command_counter; + uint8_t command_id; + uint8_t device_status; //@see stick20::device_status + uint8_t progress_bar_value; + } storage_status __packed; } __packed; uint32_t crc; @@ -238,10 +238,10 @@ namespace nitrokey { resp.command_id >= stick20::CMD_START_VALUE && resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + - std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG_L2); + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); resp.last_command_status = static_cast(stick10::command_status::ok); - switch (static_cast(resp.StorageStatus.Status_u8)) { + switch (static_cast(resp.storage_status.device_status)) { case stick20::device_status::idle : case stick20::device_status::ok: resp.device_status = static_cast(stick10::device_status::ok); @@ -256,8 +256,8 @@ namespace nitrokey { break; default: Log::instance()(std::string("Unknown storage device status, cannot translate: ") + - std::to_string(resp.StorageStatus.Status_u8), Loglevel::DEBUG); - resp.device_status = resp.StorageStatus.Status_u8; + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG); + resp.device_status = resp.storage_status.device_status; break; }; } -- cgit v1.2.1 From b53d6455d1c7dfd24ec2cbb18d5e943c0472d34e Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 18 Oct 2016 15:36:54 +0200 Subject: Code refactoring: replacing magic numbers Signed-off-by: Szczepan Zalega --- include/device_proto.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 128eca8..6b06a16 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -264,8 +264,9 @@ namespace nitrokey { //SENDPASSWORD gives wrong CRC , for now rely on !=0 (TODO report) // if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; - if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isValid()) break; - if (resp.device_status == 1) { + if (resp.device_status == static_cast(stick10::device_status::ok) && + resp.last_command_crc == outp.crc && resp.isValid()) break; + if (resp.device_status == static_cast(stick10::device_status::busy)) { receiving_retry_counter++; Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); @@ -307,7 +308,7 @@ namespace nitrokey { if (receiving_retry_counter <= 0) throw std::runtime_error( "Maximum receiving_retry_counter count reached for receiving response from the device!"); - if (resp.last_command_status != 0) + if (resp.last_command_status != static_cast(stick10::command_status::ok)) throw CommandFailedException(resp.command_id, resp.last_command_status); -- cgit v1.2.1 From 7d579ca0ca59095596275f7b891dacce54398f1f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 19 Oct 2016 09:05:25 +0200 Subject: Add all devices' and commands' statuses Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/device_proto.h') diff --git a/include/device_proto.h b/include/device_proto.h index 6b06a16..45f165b 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -251,7 +251,7 @@ namespace nitrokey { resp.device_status = static_cast(stick10::device_status::busy); break; case stick20::device_status::wrong_password: - resp.last_command_status = static_cast(stick10::device_status::wrong_password); + resp.last_command_status = static_cast(stick10::command_status::wrong_password); resp.device_status = static_cast(stick10::device_status::ok); break; default: -- cgit v1.2.1