-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- project compiles, but not creating an executable
- Loading branch information
Robert Scheinpflug
committed
Sep 13, 2015
1 parent
6c71d9c
commit b8c3f00
Showing
14 changed files
with
1,106 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#include "insomniac.h" | ||
#include <QDebug> | ||
|
||
#include <errno.h> | ||
#include <stdio.h> | ||
|
||
Insomniac::Insomniac(QQuickItem *parent): | ||
QQuickItem(parent) | ||
, m_interval(0) | ||
, m_timerWindow(120) | ||
, m_running(false) | ||
, m_repeat(true) | ||
, m_iphbdHandler(0) | ||
, m_notifier(0) | ||
{ | ||
m_iphbdHandler = iphb_open(0); | ||
|
||
if (!m_iphbdHandler) { | ||
m_lastError = Insomniac::InternalError; | ||
qDebug() << "iphb_open error" << m_iphbdHandler<< errno <<strerror(errno); | ||
return; | ||
} | ||
|
||
int sockfd = iphb_get_fd(m_iphbdHandler); | ||
if (!(sockfd > -1)) { | ||
m_lastError = Insomniac::InternalError; | ||
qDebug() << "socket failure"<<strerror(errno); | ||
return; | ||
} | ||
|
||
m_notifier = new QSocketNotifier(sockfd, QSocketNotifier::Read); | ||
if (!QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(heartbeatReceived(int)))) { | ||
delete m_notifier, m_notifier = 0; | ||
m_lastError = Insomniac::TimerFailed; | ||
qDebug() << "timer failure"; | ||
return; | ||
} | ||
m_notifier->setEnabled(false); | ||
} | ||
|
||
Insomniac::~Insomniac() | ||
{ | ||
if(m_iphbdHandler) { | ||
(void)iphb_close(m_iphbdHandler); | ||
} | ||
|
||
if(m_notifier) { | ||
delete m_notifier; | ||
} | ||
} | ||
|
||
void Insomniac::wokeUp() | ||
{ | ||
if (!m_running) | ||
return; | ||
|
||
if (!(m_iphbdHandler && m_notifier)) { | ||
m_lastError = Insomniac::InternalError; | ||
emit error(m_lastError); | ||
return; | ||
} | ||
|
||
m_notifier->setEnabled(false); | ||
|
||
(void)iphb_I_woke_up(m_iphbdHandler); | ||
|
||
m_running = false; | ||
emit runningChanged(); | ||
m_lastError = Insomniac::NoError; | ||
|
||
start(); | ||
} | ||
|
||
int Insomniac::interval() const | ||
{ | ||
return m_interval; | ||
} | ||
|
||
void Insomniac::setInterval(int seconds) | ||
{ | ||
m_interval = seconds; | ||
} | ||
|
||
void Insomniac::setRepeat(bool repeat) | ||
{ | ||
m_repeat = repeat; | ||
} | ||
|
||
bool Insomniac::repeat() const | ||
{ | ||
return m_repeat; | ||
} | ||
|
||
int Insomniac::timerWindow() const | ||
{ | ||
return m_timerWindow; | ||
} | ||
|
||
/** | ||
Sets the timer's timeout window in seconds. | ||
The timeout window is a window of time set around the interval in which the timer will timeout. | ||
It is wise to have timeout window quite big so all users of this service get synced. | ||
For example if your preferred wait is 120 seconds and you can wait anywhere within 10 seconds, | ||
use interval of 120 and timerWindow of 10. This means the timer will timeout anywhere from | ||
115 seconds to 125 seconds. | ||
*/ | ||
void Insomniac::setTimerWindow(int seconds) | ||
{ | ||
m_timerWindow = seconds; | ||
} | ||
|
||
void Insomniac::start(int interval, int timeWindow) | ||
{ | ||
m_interval = interval; | ||
m_timerWindow = timeWindow; | ||
|
||
start(); | ||
} | ||
|
||
void Insomniac::start() | ||
{ | ||
if (m_running) { | ||
return; | ||
} | ||
|
||
if (!(m_iphbdHandler && m_notifier)) { | ||
m_lastError = Insomniac::InternalError; | ||
emit error(m_lastError); | ||
return; | ||
} | ||
|
||
int mustWait = 0; | ||
time_t unixTime = iphb_wait(m_iphbdHandler, m_interval - (m_timerWindow * .5) | ||
, m_interval + (m_timerWindow * .5) , mustWait); | ||
|
||
if (unixTime == (time_t)-1) { | ||
m_lastError = Insomniac::TimerFailed; | ||
emit error(m_lastError); | ||
return; | ||
} | ||
|
||
m_notifier->setEnabled(true); | ||
m_running = true; | ||
emit runningChanged(); | ||
m_lastError = Insomniac::NoError; | ||
} | ||
|
||
void Insomniac::stop() | ||
{ | ||
if (!m_running) { | ||
return; | ||
} | ||
|
||
if (!(m_iphbdHandler && m_notifier)) { | ||
m_lastError = Insomniac::InternalError; | ||
emit error(m_lastError); | ||
return; | ||
} | ||
|
||
m_notifier->setEnabled(false); | ||
|
||
(void)iphb_discard_wakeups(m_iphbdHandler); | ||
|
||
m_running = false; | ||
emit runningChanged(); | ||
m_lastError = Insomniac::NoError; | ||
} | ||
|
||
void Insomniac::heartbeatReceived(int sock) { | ||
Q_UNUSED(sock); | ||
|
||
stop(); | ||
emit timeout(); | ||
|
||
if (m_repeat) { | ||
start(); | ||
} | ||
} | ||
|
||
bool Insomniac::running() const | ||
{ | ||
return m_running; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#ifndef INSOMNIAC_H | ||
#define INSOMNIAC_H | ||
|
||
#include <QQuickItem> | ||
#include <QSocketNotifier> | ||
|
||
extern "C" { | ||
#include "libiphb/libiphb.h" | ||
} | ||
|
||
class Insomniac : public QQuickItem | ||
{ | ||
Q_OBJECT | ||
Q_DISABLE_COPY(Insomniac) | ||
|
||
public: | ||
Insomniac(QQuickItem *parent = 0); | ||
~Insomniac(); | ||
|
||
Q_PROPERTY(int timerWindow READ timerWindow WRITE setTimerWindow) | ||
Q_PROPERTY(bool repeat READ repeat WRITE setRepeat) | ||
Q_PROPERTY(bool running READ running NOTIFY runningChanged) | ||
Q_PROPERTY(int interval READ interval WRITE setInterval) | ||
|
||
enum InsomniacError { | ||
NoError = 0, | ||
InvalidArgument, | ||
TimerFailed, | ||
InternalError | ||
}; | ||
|
||
Insomniac::InsomniacError m_lastError; | ||
InsomniacError lastError() const; | ||
|
||
void wokeUp(); | ||
|
||
int interval() const; | ||
void setInterval(int seconds); | ||
|
||
int timerWindow() const; | ||
void setTimerWindow(int seconds); | ||
|
||
bool repeat() const; | ||
void setRepeat(bool repeat); | ||
|
||
bool running() const; | ||
|
||
Q_SIGNALS: | ||
void runningChanged(); | ||
void timeout(); | ||
void error(Insomniac::InsomniacError error); | ||
|
||
private: | ||
int m_interval; | ||
int m_timerWindow; | ||
bool m_running; | ||
bool m_repeat; | ||
iphb_t m_iphbdHandler; | ||
QSocketNotifier *m_notifier; | ||
|
||
public Q_SLOTS: | ||
Q_INVOKABLE void start(int interval, int timerWindow); | ||
Q_INVOKABLE void start(); | ||
Q_INVOKABLE void stop(); | ||
|
||
private Q_SLOTS: | ||
void heartbeatReceived(int sock); | ||
}; | ||
|
||
#endif // INSOMNIAC_H | ||
|
Oops, something went wrong.