diff options
Diffstat (limited to 'src/reconnecttimer.cpp')
-rw-r--r-- | src/reconnecttimer.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/reconnecttimer.cpp b/src/reconnecttimer.cpp new file mode 100644 index 0000000..eaf6cbb --- /dev/null +++ b/src/reconnecttimer.cpp @@ -0,0 +1,64 @@ +#include <QtCore/QDebug> + +extern "C" { + #include <iphbd/libiphb.h> +} + +#include "reconnecttimer.h" + +static unsigned int num_wait_times = 8; +static unsigned short wait_times[8] = { + 2, 5, 10, 30, 2 * 60, 5 * 60, 10 * 60, 15 * 60 +}; + +ReconnectTimer::ReconnectTimer(QObject *parent) + : QObject(parent), + _iphb(iphb_open(0)), + _notifier(new QSocketNotifier(iphb_get_fd(_iphb), QSocketNotifier::Read, this)), + _active(false) +{ + connect(_notifier, &QSocketNotifier::activated, this, &ReconnectTimer::handleIphbActivity); +} + +ReconnectTimer::~ReconnectTimer() +{ + iphb_close(_iphb); +} + +void ReconnectTimer::start() +{ + _active = true; + _counter = 0; + setupWait(); +} + +void ReconnectTimer::stop() +{ + _active = false; + _counter = 0; + iphb_wait(_iphb, 0, 0, 0); +} + +void ReconnectTimer::setupWait() +{ + iphb_wait(_iphb, wait_times[_counter] / 2, wait_times[_counter], 0); +} + +void ReconnectTimer::handleIphbActivity() +{ + iphb_discard_wakeups(_iphb); + + qDebug() << "iphb wakeup"; + + if (!_active) { + // False awakening + return; + } + + emit tick(); + + if (++_counter > num_wait_times) + _counter = num_wait_times; + + setupWait(); +} |