Skip to content

Commit

Permalink
Correct device counters
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 a0df25c commit 3d96df2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
21 changes: 15 additions & 6 deletions device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,23 @@ Stick20::Stick20():
#define p(x) ss << #x << " " << x << ", ";
std::string Device::ErrorCounters::get_as_string() {
std::stringstream ss;
p(wrong_CRC);
p(CRC_other_than_awaited);
p(busy);
p(total_comm_runs);
p(communication_successful);
ss << "(";
p(command_successful_recv);
p(command_result_not_equal_0_recv);
ss << "), ";
p(sends_executed);
p(recv_executed);
p(successful_storage_commands);
p(total_retries);
ss << "(";
p(busy);
p(busy_progressbar);
p(CRC_other_than_awaited);
p(wrong_CRC);
ss << "), ";
p(sending_error);
p(receiving_error);
p(total_comm_runs);
p(storage_commands);
p(successful );
return ss.str();
}
29 changes: 19 additions & 10 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,30 @@ enum class DeviceModel{
STORAGE
};

#include <atomic>

class Device {

public:

struct ErrorCounters{
int wrong_CRC;
int CRC_other_than_awaited;
int busy;
int total_retries;
int sending_error;
int receiving_error;
int total_comm_runs;
int storage_commands;
int successful;
using cnt = std::atomic_int;
cnt wrong_CRC;
cnt CRC_other_than_awaited;
cnt busy;
cnt total_retries;
cnt sending_error;
cnt receiving_error;
cnt total_comm_runs;
cnt successful_storage_commands;
cnt command_successful_recv;
cnt recv_executed;
cnt sends_executed;
cnt busy_progressbar;
cnt command_result_not_equal_0_recv;
cnt communication_successful;
std::string get_as_string();

} m_counters = {};


Expand Down Expand Up @@ -71,7 +80,7 @@ class Device {
bool is_connected();

void show_stats();
ErrorCounters get_stats(){ return m_counters; }
// ErrorCounters get_stats(){ return m_counters; }
int get_retry_receiving_count() const { return m_retry_receiving_count; };
int get_retry_sending_count() const { return m_retry_sending_count; };
std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; };
Expand Down
18 changes: 15 additions & 3 deletions include/device_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ namespace nitrokey {
int receiving_retry_counter = 0;
int sending_retry_counter = dev->get_retry_sending_count();
while (sending_retry_counter-- > 0) {
dev->m_counters.sends_executed++;
status = dev->send(&outp);
if (status <= 0){
//FIXME early disconnection not yet working properly
Expand All @@ -261,14 +262,14 @@ namespace nitrokey {
// FIXME make checks done in device:recv here
receiving_retry_counter = dev->get_retry_receiving_count();
while (receiving_retry_counter-- > 0) {
dev->m_counters.recv_executed++;
status = dev->recv(&resp);

if (dev->get_device_model() == DeviceModel::STORAGE &&
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.storage_status.device_status), Loglevel::DEBUG_L2);
dev->m_counters.storage_commands++;

resp.last_command_status = static_cast<uint8_t>(stick10::command_status::ok);
switch (static_cast<stick20::device_status>(resp.storage_status.device_status)) {
Expand Down Expand Up @@ -362,6 +363,7 @@ namespace nitrokey {
if (resp.device_status == static_cast<uint8_t>(stick10::device_status::busy) &&
static_cast<stick20::device_status>(resp.storage_status.device_status)
== stick20::device_status::busy_progressbar){
dev->m_counters.busy_progressbar++;
throw LongOperationInProgressException(
resp.command_id, resp.device_status, resp.storage_status.progress_bar_value);
}
Expand All @@ -370,10 +372,20 @@ 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 != static_cast<uint8_t>(stick10::command_status::ok))
dev->m_counters.communication_successful++;

if (resp.last_command_status != static_cast<uint8_t>(stick10::command_status::ok)){
dev->m_counters.command_result_not_equal_0_recv++;
throw CommandFailedException(resp.command_id, resp.last_command_status);
}

dev->m_counters.successful++;
dev->m_counters.command_successful_recv++;

if (dev->get_device_model() == DeviceModel::STORAGE &&
resp.command_id >= stick20::CMD_START_VALUE &&
resp.command_id < stick20::CMD_END_VALUE ) {
dev->m_counters.successful_storage_commands++;
}

// See: DeviceResponse
return resp;
Expand Down

0 comments on commit 3d96df2

Please sign in to comment.