src/location/qgeoinfothread_wince.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QMetaType>
       
    43 
       
    44 #include "qgeoinfothread_wince_p.h"
       
    45 
       
    46 Q_DECLARE_METATYPE(GPS_POSITION);
       
    47 
       
    48 QTM_BEGIN_NAMESPACE
       
    49 
       
    50 // ========== QGeoInfoValidator ==========
       
    51 
       
    52 QGeoInfoValidator::QGeoInfoValidator() {}
       
    53 
       
    54 QGeoInfoValidator::~QGeoInfoValidator() {}
       
    55 
       
    56 // ========== QGeoInfoThreadWinCE ==========
       
    57 
       
    58 
       
    59 // This QGeoInfoThreadWinCE instance takes ownership of the validator, and must delete it before
       
    60 // it is destructed.
       
    61 QGeoInfoThreadWinCE::QGeoInfoThreadWinCE(QGeoInfoValidator *validator, bool timeoutsForPeriodicUpdates, QObject *parent)
       
    62         : QThread(parent),
       
    63         validator(validator),
       
    64         timeoutsForPeriodicUpdates(timeoutsForPeriodicUpdates),
       
    65         requestScheduled(false),
       
    66         requestInterval(0),
       
    67         updatesScheduled(false),
       
    68         updatesInterval(0),
       
    69         stopping(false),
       
    70         hasLastPosition(false),
       
    71         invalidDataReceived(false),
       
    72         updateTimeoutTriggered(false)
       
    73 {
       
    74     qRegisterMetaType<GPS_POSITION>();
       
    75     m_newDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    76     m_gpsStateChange = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    77     m_wakeUpEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    78 }
       
    79 
       
    80 QGeoInfoThreadWinCE::~QGeoInfoThreadWinCE()
       
    81 {
       
    82     // Let/make the thread finish...
       
    83 
       
    84     mutex.lock();
       
    85 
       
    86     updatesScheduled = false;
       
    87     requestScheduled = false;
       
    88     stopping = true;
       
    89 
       
    90     mutex.unlock();
       
    91 
       
    92     wakeUp();
       
    93 
       
    94     wait();
       
    95 
       
    96     // ...then clean up.
       
    97 
       
    98     delete validator;
       
    99 
       
   100     CloseHandle(m_newDataEvent);
       
   101     CloseHandle(m_gpsStateChange);
       
   102     CloseHandle(m_wakeUpEvent);
       
   103 }
       
   104 
       
   105 // TODO - just need to add the WinCE line from QTime::currentTime to QDateTime::currentDateTime
       
   106 // and we can scrap this method
       
   107 QDateTime QGeoInfoThreadWinCE::currentDateTime()
       
   108 {
       
   109     QDate d = QDate::currentDate();
       
   110     QTime t = QTime::currentTime();
       
   111 
       
   112     // just in case we past midnight in between the last two calls
       
   113     if (d != QDate::currentDate()) {
       
   114         d = QDate::currentDate();
       
   115         t = QTime::currentTime();
       
   116     }
       
   117 
       
   118     return QDateTime(d, t);
       
   119 }
       
   120 
       
   121 int QGeoInfoThreadWinCE::msecsTo(QDateTime from, QDateTime to)
       
   122 {
       
   123     int MSECS_PER_DAY = 86400000;
       
   124     return (from.date().daysTo(to.date()) * MSECS_PER_DAY) + from.time().msecsTo(to.time());
       
   125 }
       
   126 
       
   127 void QGeoInfoThreadWinCE::requestUpdate(int timeout)
       
   128 {
       
   129     QMutexLocker locker(&mutex);
       
   130 
       
   131     if (!requestScheduled) {
       
   132         requestScheduled = true;
       
   133 
       
   134         if (timeout == 0)
       
   135             timeout = DefaultRequestTimeout;
       
   136 
       
   137         requestInterval = timeout;
       
   138         requestNextTime = currentDateTime().addMSecs(requestInterval);
       
   139 
       
   140         locker.unlock();
       
   141         wakeUp();
       
   142     }
       
   143 }
       
   144 
       
   145 void QGeoInfoThreadWinCE::startUpdates()
       
   146 {
       
   147     QMutexLocker locker(&mutex);
       
   148     if (!updatesScheduled) {
       
   149         updatesScheduled = true;
       
   150         updateTimeoutTriggered = false;
       
   151         hasLastPosition = false;
       
   152 
       
   153         if (updatesInterval != 0)
       
   154             updatesNextTime = currentDateTime().addMSecs(updatesInterval);
       
   155 
       
   156         locker.unlock();
       
   157         wakeUp();
       
   158     }
       
   159 }
       
   160 
       
   161 void QGeoInfoThreadWinCE::stopUpdates()
       
   162 {
       
   163     QMutexLocker locker(&mutex);
       
   164     if (updatesScheduled) {
       
   165         updatesScheduled = false;
       
   166         locker.unlock();
       
   167         wakeUp();
       
   168     }
       
   169 }
       
   170 
       
   171 void QGeoInfoThreadWinCE::setUpdateInterval(int interval)
       
   172 {
       
   173     QMutexLocker locker(&mutex);
       
   174 
       
   175     if (interval == updatesInterval)
       
   176         return;
       
   177 
       
   178     int oldInterval = updatesInterval;
       
   179     updatesInterval = interval;
       
   180 
       
   181     if (updatesScheduled) {
       
   182         QDateTime now = currentDateTime();
       
   183 
       
   184         // The periodic update interval has been changed and updates are still ocurring.
       
   185 
       
   186         if (oldInterval != 0) {
       
   187             if (updatesInterval != 0) {
       
   188                 // If we are changing fixed intervals we update the scheduled time for the next
       
   189                 // periodic update, making sure that it is scheduled in the future.
       
   190                 updatesNextTime = updatesNextTime.addMSecs(updatesInterval - oldInterval);
       
   191                 while (msecsTo(now, updatesNextTime) < 0)
       
   192                     updatesNextTime = updatesNextTime.addMSecs(updatesInterval);
       
   193             } else {
       
   194                 // If we now want to emit updates as the data arrives we invalidate the scheduled
       
   195                 // time for the next update, just to be on the safe side.
       
   196                 updatesNextTime = now;
       
   197             }
       
   198         } else {
       
   199             // If we were previously emitting updates as the data arrived we set the scheduled time
       
   200             // for the next periodic update.
       
   201             updatesNextTime = now.addMSecs(updatesInterval);
       
   202         }
       
   203 
       
   204         locker.unlock();
       
   205         wakeUp();
       
   206     }
       
   207 }
       
   208 
       
   209 void QGeoInfoThreadWinCE::wakeUp()
       
   210 {
       
   211     SetEvent(m_wakeUpEvent);
       
   212     statusUpdated.wakeAll();
       
   213 }
       
   214 
       
   215 // We try to keep the GPS turned off as much as we can to preserve battery life.
       
   216 // When run() is called we turn on the GPS device and we leave it on
       
   217 // until the request is satisfied or periodic updates are stopped.
       
   218 // The methods requestUpdate() and startUpdates() will call start() if required.
       
   219 void QGeoInfoThreadWinCE::run()
       
   220 {
       
   221     mutex.lock();
       
   222     gpsReachedOnState = false;
       
   223     m_gps = NULL;
       
   224 
       
   225     const int handleCount = 3;
       
   226     HANDLE handles[handleCount] = { m_newDataEvent, m_gpsStateChange, m_wakeUpEvent };
       
   227 
       
   228     if (updatesScheduled || requestScheduled) {
       
   229         m_gps = GPSOpenDevice(m_newDataEvent, m_gpsStateChange, NULL, 0);
       
   230     }
       
   231 
       
   232     while (true) {
       
   233 
       
   234         if (stopping)
       
   235             break;
       
   236 
       
   237         if (!updatesScheduled && !requestScheduled) {
       
   238             if (m_gps != NULL) {
       
   239                 GPSCloseDevice(m_gps);
       
   240                 m_gps = NULL;
       
   241             }
       
   242             statusUpdated.wait(&mutex);
       
   243             if (updatesScheduled || requestScheduled) {
       
   244                 gpsReachedOnState = false;
       
   245                 m_gps = GPSOpenDevice(m_newDataEvent, m_gpsStateChange, NULL, 0);
       
   246             }
       
   247         }
       
   248 
       
   249         // If the periodic update is 0 then updates are returned as available.
       
   250         // If this is not the case then the next timeout will be set for whichever of
       
   251         // the request and periodic updates that is due next.
       
   252 
       
   253         // We cap the amount of time we spend waiting for updates.
       
   254         DWORD timeout = MaximumMainLoopWaitTime;
       
   255 
       
   256         QDateTime now = currentDateTime();
       
   257 
       
   258         if (requestScheduled) {
       
   259             if (!updatesScheduled || (updatesInterval == 0)
       
   260                     || (msecsTo(requestNextTime, updatesNextTime) >= 0)) {
       
   261                 timeout = msecsTo(now, requestNextTime) + 100;
       
   262             } else {
       
   263                 if (updatesInterval != 0)
       
   264                     timeout = msecsTo(now, updatesNextTime) + 100;
       
   265             }
       
   266         } else {
       
   267             // updatesScheduled has to be true or we wouldn't still be in the larger while loop.
       
   268             if (updatesInterval != 0)
       
   269                 timeout = msecsTo(now, updatesNextTime) + 100;
       
   270         }
       
   271 
       
   272         if (timeout > MaximumMainLoopWaitTime)
       
   273             timeout = MaximumMainLoopWaitTime;
       
   274 
       
   275         mutex.unlock();
       
   276         DWORD dwRet = WaitForMultipleObjects(handleCount, handles, FALSE, timeout);
       
   277         mutex.lock();
       
   278 
       
   279         // The GPS data has been updated.
       
   280         if (dwRet == WAIT_OBJECT_0) {
       
   281             // The other options are:
       
   282             // dwRet == WAIT_OBJECT_0 + 1
       
   283             //   => The GPS state has been updated.
       
   284             // dwRet == WAIT_OBJECT_0 + 2
       
   285             //   => We called QGeoInfoThreadWinCE::wakeUp().
       
   286             // dwRet == WAIT_TIMEOUT
       
   287             //   => WaitForMultipleObjects() timed out.
       
   288 
       
   289             GPS_POSITION posn;
       
   290             posn.dwVersion = GPS_VERSION_1;
       
   291             posn.dwSize = sizeof(posn);
       
   292 
       
   293             dwRet = GPSGetPosition(m_gps, &posn, timeout, 0);
       
   294 
       
   295             if (dwRet == ERROR_SUCCESS) {
       
   296                 if (!validator->valid(posn)) {
       
   297                     invalidDataReceived = true;
       
   298                 } else {
       
   299                     m_lastPosition = posn;
       
   300                     hasLastPosition = true;
       
   301                     updateTimeoutTriggered = false;
       
   302 
       
   303                     // A request and a periodic update could both be satisfied at once.
       
   304                     // We use this flag to prevent a double update.
       
   305                     bool emitDataUpdated = false;
       
   306 
       
   307                     // If a request is in process we emit the dataUpdated signal.
       
   308                     if (requestScheduled) {
       
   309                         emitDataUpdated = true;
       
   310                         requestScheduled = false;
       
   311                     }
       
   312 
       
   313                     // If we are updating as data becomes available or if the update period has elapsed
       
   314                     // we emit the dataUpdated signal.
       
   315                     if (updatesScheduled) {
       
   316                         QDateTime now = currentDateTime();
       
   317                         if (updatesInterval == 0) {
       
   318                             emitDataUpdated = true;
       
   319                         } else if (msecsTo(now, updatesNextTime) < 0) {
       
   320                             while (msecsTo(now, updatesNextTime) < 0)
       
   321                                 updatesNextTime = updatesNextTime.addMSecs(updatesInterval);
       
   322                             emitDataUpdated = true;
       
   323                         }
       
   324                     }
       
   325 
       
   326                     if (emitDataUpdated) {
       
   327                         hasLastPosition = false;
       
   328                         mutex.unlock();
       
   329                         emit dataUpdated(m_lastPosition);
       
   330                         mutex.lock();
       
   331                     }
       
   332                 }
       
   333             }
       
   334         }
       
   335         if (dwRet != WAIT_OBJECT_0 || invalidDataReceived) {
       
   336             invalidDataReceived = false;
       
   337 
       
   338             // Third party apps may have the ability to turn off the gps hardware independently of
       
   339             // the Microsoft GPS API.
       
   340             // This checks for an unexpected power down and turns the hardware back on.
       
   341 
       
   342             // The GPS state has been updated.
       
   343 
       
   344             if (dwRet == WAIT_OBJECT_0 + 1) {
       
   345                 GPS_DEVICE device;
       
   346                 device.dwVersion = GPS_VERSION_1;
       
   347                 device.dwSize = sizeof(device);
       
   348 
       
   349                 dwRet = GPSGetDeviceState(&device);
       
   350 
       
   351                 if (device.dwDeviceState == SERVICE_STATE_ON) {
       
   352                     gpsReachedOnState = true;
       
   353                 } else if ((device.dwDeviceState == SERVICE_STATE_OFF) && gpsReachedOnState) {
       
   354                     // We do not want to mess with devices that are slow starting up, so we only
       
   355                     // turn on devices that have previously reached the "On" state.
       
   356                     gpsReachedOnState = false;
       
   357                     m_gps = GPSOpenDevice(m_newDataEvent, m_gpsStateChange, NULL, 0);
       
   358                 }
       
   359             }
       
   360 
       
   361             // We reach this point if the gps state has changed, if the wake up event has been
       
   362             // triggered, if we received data we were not interested in from the GPS,
       
   363             // or if a timeout occurred while waiting for gps data.
       
   364             //
       
   365             // In all of these cases we should check for request and periodic update timeouts.
       
   366 
       
   367             QDateTime now = currentDateTime();
       
   368 
       
   369             bool emitUpdateTimeout = false;
       
   370 
       
   371             // Check for request timeouts.
       
   372             if (requestScheduled && msecsTo(now, requestNextTime) < 0) {
       
   373                 requestScheduled = false;
       
   374                 emitUpdateTimeout = true;
       
   375             }
       
   376 
       
   377             // Check to see if a periodic update is due.
       
   378             if (updatesScheduled && updatesInterval != 0 && (msecsTo(now, updatesNextTime) < 0)) {
       
   379                 while (msecsTo(now, updatesNextTime) < 0)
       
   380                     updatesNextTime = updatesNextTime.addMSecs(updatesInterval);
       
   381                 if (hasLastPosition) {
       
   382                     hasLastPosition = false;
       
   383                     mutex.unlock();
       
   384                     emit dataUpdated(m_lastPosition);
       
   385                     mutex.lock();
       
   386                 } else {
       
   387                     if (timeoutsForPeriodicUpdates && !updateTimeoutTriggered) {
       
   388                         updateTimeoutTriggered = true;
       
   389                         emitUpdateTimeout = true;
       
   390                     }
       
   391                 }
       
   392             }
       
   393 
       
   394             if (emitUpdateTimeout) {
       
   395                 mutex.unlock();
       
   396                 emit updateTimeout();
       
   397                 mutex.lock();
       
   398             }
       
   399         }
       
   400     }
       
   401 
       
   402     if (m_gps != NULL)
       
   403         GPSCloseDevice(m_gps);
       
   404 
       
   405     mutex.unlock();
       
   406 }
       
   407 
       
   408 #include "moc_qgeoinfothread_wince_p.cpp"
       
   409 QTM_END_NAMESPACE