| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the Qt Mobility Components. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qsystemalignedtimer.h" |
| 41 | #include "qsystemalignedtimer_meego_p.h" |
| 42 | #include <QDebug> |
| 43 | |
| 44 | #include <errno.h> |
| 45 | #include <stdio.h> |
| 46 | |
| 47 | QTM_BEGIN_NAMESPACE |
| 48 | |
| 49 | QSystemAlignedTimerPrivate::QSystemAlignedTimerPrivate(QObject *parent) |
| 50 | : QObject(parent) |
| 51 | , m_lastError(QSystemAlignedTimer::NoError) |
| 52 | , m_minimumInterval(0) |
| 53 | , m_maximumInterval(0) |
| 54 | , m_running(false) |
| 55 | , m_singleShot(false) |
| 56 | , m_iphbdHandler(0) |
| 57 | , m_notifier(0) |
| 58 | { |
| 59 | m_iphbdHandler = iphb_open(0); |
| 60 | |
| 61 | if (!m_iphbdHandler) { |
| 62 | m_lastError = QSystemAlignedTimer::InternalError; |
| 63 | qDebug() << "iphb_open error" << m_iphbdHandler<< errno <<strerror(errno); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | int sockfd = iphb_get_fd(m_iphbdHandler); |
| 68 | if (!(sockfd > -1)) { |
| 69 | m_lastError = QSystemAlignedTimer::InternalError; |
| 70 | qDebug() << "socket failure"<<strerror(errno); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | m_notifier = new QSocketNotifier(sockfd, QSocketNotifier::Read); |
| 75 | if (!QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(heartbeatReceived(int)))) { |
| 76 | delete m_notifier, m_notifier = 0; |
| 77 | m_lastError = QSystemAlignedTimer::TimerFailed; |
| 78 | qDebug() << "timer failure"; |
| 79 | return; |
| 80 | } |
| 81 | m_notifier->setEnabled(false); |
| 82 | } |
| 83 | |
| 84 | QSystemAlignedTimerPrivate::~QSystemAlignedTimerPrivate() |
| 85 | { |
| 86 | if (m_iphbdHandler) |
| 87 | (void)iphb_close(m_iphbdHandler); |
| 88 | |
| 89 | if (m_notifier) |
| 90 | delete m_notifier; |
| 91 | } |
| 92 | |
| 93 | void QSystemAlignedTimerPrivate::wokeUp() |
| 94 | { |
| 95 | if (!m_running) |
| 96 | return; |
| 97 | |
| 98 | if (!(m_iphbdHandler && m_notifier)) { |
| 99 | m_lastError = QSystemAlignedTimer::InternalError; |
| 100 | emit error(m_lastError); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | m_notifier->setEnabled(false); |
| 105 | |
| 106 | (void)iphb_I_woke_up(m_iphbdHandler); |
| 107 | |
| 108 | m_running = false; |
| 109 | m_lastError = QSystemAlignedTimer::NoError; |
| 110 | |
| 111 | if (!m_singleShot) |
| 112 | start(); |
| 113 | } |
| 114 | |
| 115 | int QSystemAlignedTimerPrivate::minimumInterval() const |
| 116 | { |
| 117 | return m_minimumInterval; |
| 118 | } |
| 119 | |
| 120 | void QSystemAlignedTimerPrivate::setMinimumInterval(int seconds) |
| 121 | { |
| 122 | m_minimumInterval = seconds; |
| 123 | } |
| 124 | |
| 125 | int QSystemAlignedTimerPrivate::maximumInterval() const |
| 126 | { |
| 127 | return m_maximumInterval; |
| 128 | } |
| 129 | |
| 130 | void QSystemAlignedTimerPrivate::setMaximumInterval(int seconds) |
| 131 | { |
| 132 | m_maximumInterval = seconds; |
| 133 | } |
| 134 | |
| 135 | void QSystemAlignedTimerPrivate::setSingleShot(bool singleShot) |
| 136 | { |
| 137 | m_singleShot = singleShot; |
| 138 | } |
| 139 | |
| 140 | bool QSystemAlignedTimerPrivate::isSingleShot() const |
| 141 | { |
| 142 | return m_singleShot; |
| 143 | } |
| 144 | |
| 145 | void QSystemAlignedTimerPrivate::singleShot(int minimumTime, int maximumTime, QObject *receiver, const char *member) |
| 146 | { |
| 147 | if (receiver && member) { |
| 148 | QSystemAlignedTimerPrivate *alignedTimer = new QSystemAlignedTimerPrivate(receiver); |
| 149 | |
| 150 | alignedTimer->m_singleShot = true; |
| 151 | |
| 152 | connect(alignedTimer, SIGNAL(timeout()), receiver, member); |
| 153 | alignedTimer->start(minimumTime, maximumTime); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | QSystemAlignedTimer::AlignedTimerError QSystemAlignedTimerPrivate::lastError() const |
| 158 | { |
| 159 | return m_lastError; |
| 160 | } |
| 161 | |
| 162 | void QSystemAlignedTimerPrivate::start(int minimumTime, int maximumTime) |
| 163 | { |
| 164 | m_minimumInterval = minimumTime; |
| 165 | m_maximumInterval = maximumTime; |
| 166 | |
| 167 | start(); |
| 168 | } |
| 169 | |
| 170 | void QSystemAlignedTimerPrivate::start() |
| 171 | { |
| 172 | if (m_running) |
| 173 | return; |
| 174 | |
| 175 | if (!(m_iphbdHandler && m_notifier)) { |
| 176 | m_lastError = QSystemAlignedTimer::InternalError; |
| 177 | emit error(m_lastError); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | int mustWait = 0; |
| 182 | time_t unixTime = iphb_wait(m_iphbdHandler, m_minimumInterval, m_maximumInterval, mustWait); |
| 183 | |
| 184 | if (unixTime == (time_t)-1) { |
| 185 | m_lastError = QSystemAlignedTimer::TimerFailed; |
| 186 | emit error(m_lastError); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | m_notifier->setEnabled(true); |
| 191 | m_running = true; |
| 192 | m_lastError = QSystemAlignedTimer::NoError; |
| 193 | } |
| 194 | |
| 195 | void QSystemAlignedTimerPrivate::stop() |
| 196 | { |
| 197 | if (!m_running) |
| 198 | return; |
| 199 | |
| 200 | if (!(m_iphbdHandler && m_notifier)) { |
| 201 | m_lastError = QSystemAlignedTimer::InternalError; |
| 202 | emit error(m_lastError); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | m_notifier->setEnabled(false); |
| 207 | |
| 208 | (void)iphb_discard_wakeups(m_iphbdHandler); |
| 209 | |
| 210 | m_running = false; |
| 211 | m_lastError = QSystemAlignedTimer::NoError; |
| 212 | } |
| 213 | |
| 214 | void QSystemAlignedTimerPrivate::heartbeatReceived(int sock) { |
| 215 | Q_UNUSED(sock); |
| 216 | |
| 217 | stop(); |
| 218 | emit timeout(); |
| 219 | |
| 220 | if (!m_singleShot) |
| 221 | start(); |
| 222 | } |
| 223 | |
| 224 | bool QSystemAlignedTimerPrivate::isActive() const |
| 225 | { |
| 226 | return m_running; |
| 227 | } |
| 228 | |
| 229 | #include "moc_qsystemalignedtimer_meego_p.cpp" |
| 230 | |
| 231 | QTM_END_NAMESPACE |