From 9e6930b2fe836f240de8ac78c03d54e7f0214122 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 17 May 2021 14:28:04 +0300 Subject: device: fix timeout logic when Device is reused --- src/voltronic/device.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/voltronic/device.cc b/src/voltronic/device.cc index 632ed27..eb332eb 100644 --- a/src/voltronic/device.cc +++ b/src/voltronic/device.cc @@ -33,7 +33,6 @@ void Device::setVerbose(bool verbose) { void Device::setTimeout(u64 timeout) { timeout_ = timeout; - timeStarted_ = timestamp(); } u64 Device::getElapsedTime() const { @@ -44,10 +43,16 @@ u64 Device::getTimeLeft() const { if (!timeout_) return std::numeric_limits::max(); - return std::max((u64)0, timeout_ - getElapsedTime()); + u64 elapsed = getElapsedTime(); + if (elapsed > timeout_) + return 0; + + return timeout_ - elapsed; } size_t Device::run(const u8* inbuf, size_t inbufSize, u8* outbuf, size_t outbufSize) { + timeStarted_ = timestamp(); + send(inbuf, inbufSize); if (!getTimeLeft()) -- cgit v1.2.3