summaryrefslogtreecommitdiff
path: root/src/reconnecttimer.cpp
blob: a58a13e8b9ae0e28fc3dfd5faabda2ba8b13e00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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),
	  _counter(0)
{
	connect(_notifier, &QSocketNotifier::activated, this, &ReconnectTimer::handleIphbActivity);
}

ReconnectTimer::~ReconnectTimer()
{
	_active = false;
	_iphb = iphb_close(_iphb);
}

void ReconnectTimer::scheduleNextAttempt()
{
	_active = true;

	time_t res = iphb_wait2(_iphb, wait_times[_counter] / 2, wait_times[_counter], 0, 0);

	if (res == -1) {
		qErrnoWarning("Failed to iphb_wait");
	}
}

void ReconnectTimer::stop()
{
	_active = false;
	_counter = 0;
}

void ReconnectTimer::handleIphbActivity()
{
	iphb_discard_wakeups(_iphb);

	qDebug() << "iphb wakeup";

	if (!_active) {
		// False awakening
		return;
	}

	_active = false;
	if (++_counter > num_wait_times)
		_counter = num_wait_times;

	emit tryReconnect();
}