| 1 |
/* |
| 2 |
* Copyright 2013 Canonical Ltd. |
| 3 |
* Copyright 2022 UBports Foundation. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation; version 3. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Thomas Voß <[email protected]> |
| 18 |
* Marius Gripsgard <[email protected]> |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "core_geo_position_info_source.h" |
| 22 |
|
| 23 |
#include <cmath> |
| 24 |
#include <chrono> |
| 25 |
|
| 26 |
#include <QGuiApplication> |
| 27 |
#include <QtCore> |
| 28 |
|
| 29 |
#include "instance.h" |
| 30 |
|
| 31 |
#include <com/lomiri/location/service/session/interface.h> |
| 32 |
#include <com/lomiri/location/heading.h> |
| 33 |
#include <com/lomiri/location/position.h> |
| 34 |
#include <com/lomiri/location/velocity.h> |
| 35 |
#include <com/lomiri/location/update.h> |
| 36 |
|
| 37 |
namespace cll = com::lomiri::location; |
| 38 |
namespace cllss = com::lomiri::location::service::session; |
| 39 |
|
| 40 |
struct core::GeoPositionInfoSource::Private |
| 41 |
{ |
| 42 |
// If an application requests an individual position update with a |
| 43 |
// timeout value of 0, we bump the timeout to the default value of |
| 44 |
// 10 seconds. |
| 45 |
static const unsigned int default_timeout_in_ms = 10 * 1000; |
| 46 |
|
| 47 |
// Processes the incoming position update and translates it to Qt world. |
| 48 |
void handlePositionUpdate(const cll::Update<cll::Position>& position); |
| 49 |
|
| 50 |
// Processes the incoming heading update and translates it to Qt world. |
| 51 |
void handleHeadingUpdate(const cll::Update<cll::Heading>& heading); |
| 52 |
|
| 53 |
// Processes the incoming velocity update and translates it to Qt world. |
| 54 |
void handleVelocityUpdate(const cll::Update<cll::Velocity>& velocity); |
| 55 |
|
| 56 |
void createLocationServiceSession(); |
| 57 |
void destroyLocationServiceSession(); |
| 58 |
|
| 59 |
// Creates a new instance and attempts to connect to the background service. |
| 60 |
// Stores errors in the error member. |
| 61 |
Private(core::GeoPositionInfoSource* parent); |
| 62 |
~Private(); |
| 63 |
|
| 64 |
core::GeoPositionInfoSource* parent; |
| 65 |
cllss::Interface::Ptr session; |
| 66 |
std::shared_ptr<Instance> instance; |
| 67 |
QMutex lastKnownPositionGuard; |
| 68 |
QGeoPositionInfo lastKnownPosition; |
| 69 |
QTimer timer; |
| 70 |
QGeoPositionInfoSource::Error error; |
| 71 |
}; |
| 72 |
|
| 73 |
core::GeoPositionInfoSource::GeoPositionInfoSource(QObject *parent) |
| 74 |
: QGeoPositionInfoSource(parent), |
| 75 |
m_applicationActive(true), |
| 76 |
m_lastReqTimeout(-1), |
| 77 |
m_state(State::stopped), |
| 78 |
d(new Private(this)) |
| 79 |
{ |
| 80 |
d->timer.setSingleShot(true); |
| 81 |
QObject::connect(&d->timer, SIGNAL(timeout()), this, SLOT(timeout()), Qt::DirectConnection); |
| 82 |
// Whenever we receive an update, we stop the timeout timer immediately. |
| 83 |
QObject::connect(this, SIGNAL(positionUpdated(const QGeoPositionInfo&)), &d->timer, SLOT(stop())); |
| 84 |
QObject::connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(applicationStateChanged())); |
| 85 |
qRegisterMetaType<Qt::ApplicationState>("Qt::ApplicationState"); |
| 86 |
} |
| 87 |
|
| 88 |
void core::GeoPositionInfoSource::applicationStateChanged() |
| 89 |
{ |
| 90 |
Qt::ApplicationState state = qApp->applicationState(); |
| 91 |
if (state == Qt::ApplicationInactive) { |
| 92 |
if (m_applicationActive) { |
| 93 |
int state = m_state; |
| 94 |
stopUpdates(); |
| 95 |
|
| 96 |
m_applicationActive = false; |
| 97 |
if (state == State::one_shot) { |
| 98 |
// Save current time out |
| 99 |
if (d->timer.isActive()) { |
| 100 |
m_lastReqTimeout = d->timer.interval(); |
| 101 |
d->timer.stop(); |
| 102 |
} |
| 103 |
} |
| 104 |
else if (state == State::running) { |
| 105 |
// Stop continuous updates and suspend |
| 106 |
m_state = State::suspended; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
else if (state == Qt::ApplicationActive) { |
| 111 |
if (!m_applicationActive) { |
| 112 |
m_applicationActive = true; |
| 113 |
|
| 114 |
// Only restart updates if active before suspending |
| 115 |
if (m_lastReqTimeout > -1) { |
| 116 |
requestUpdate(m_lastReqTimeout); |
| 117 |
m_lastReqTimeout = -1; |
| 118 |
} |
| 119 |
else if (m_state == State::suspended) { |
| 120 |
// Restart continuous updates |
| 121 |
startUpdates(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
core::GeoPositionInfoSource::~GeoPositionInfoSource() |
| 129 |
{ |
| 130 |
} |
| 131 |
|
| 132 |
void core::GeoPositionInfoSource::setUpdateInterval(int msec) |
| 133 |
{ |
| 134 |
// We emit our current error state whenever a caller tries to interact |
| 135 |
// with the source although we are in error state. |
| 136 |
if (error() != QGeoPositionInfoSource::NoError) |
| 137 |
{ |
| 138 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 139 |
Q_EMIT(QGeoPositionInfoSource::errorOccurred(d->error)); |
| 140 |
#else |
| 141 |
Q_EMIT(QGeoPositionInfoSource::error(d->error)); |
| 142 |
#endif |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
(void) msec; |
| 147 |
} |
| 148 |
|
| 149 |
void core::GeoPositionInfoSource::setPreferredPositioningMethods(PositioningMethods methods) |
| 150 |
{ |
| 151 |
// We emit our current error state whenever a caller tries to interact |
| 152 |
// with the source although we are in error state. |
| 153 |
if (error() != QGeoPositionInfoSource::NoError) |
| 154 |
{ |
| 155 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 156 |
Q_EMIT(QGeoPositionInfoSource::errorOccurred(d->error)); |
| 157 |
#else |
| 158 |
Q_EMIT(QGeoPositionInfoSource::error(d->error)); |
| 159 |
#endif |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
QGeoPositionInfoSource::setPreferredPositioningMethods(methods); |
| 164 |
} |
| 165 |
|
| 166 |
QGeoPositionInfo core::GeoPositionInfoSource::lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const |
| 167 |
{ |
| 168 |
// We emit our current error state whenever a caller tries to interact |
| 169 |
// with the source although we are in error state. |
| 170 |
if (error() != QGeoPositionInfoSource::NoError) |
| 171 |
return QGeoPositionInfo(); |
| 172 |
|
| 173 |
Q_UNUSED(fromSatellitePositioningMethodsOnly); |
| 174 |
QMutexLocker lock(&d->lastKnownPositionGuard); |
| 175 |
return QGeoPositionInfo(d->lastKnownPosition); |
| 176 |
} |
| 177 |
|
| 178 |
QGeoPositionInfoSource::PositioningMethods core::GeoPositionInfoSource::supportedPositioningMethods() const |
| 179 |
{ |
| 180 |
// We emit our current error state whenever a caller tries to interact |
| 181 |
// with the source although we are in error state. |
| 182 |
if (error() != QGeoPositionInfoSource::NoError) |
| 183 |
{ |
| 184 |
return QGeoPositionInfoSource::NoPositioningMethods; |
| 185 |
} |
| 186 |
|
| 187 |
return AllPositioningMethods; |
| 188 |
} |
| 189 |
|
| 190 |
void core::GeoPositionInfoSource::startUpdates() |
| 191 |
{ |
| 192 |
if (d->session == nullptr) { |
| 193 |
d->createLocationServiceSession(); |
| 194 |
} |
| 195 |
|
| 196 |
// We emit our current error state whenever a caller tries to interact |
| 197 |
// with the source although we are in error state. |
| 198 |
if (error() != QGeoPositionInfoSource::NoError) |
| 199 |
{ |
| 200 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 201 |
Q_EMIT(QGeoPositionInfoSource::errorOccurred(d->error)); |
| 202 |
#else |
| 203 |
Q_EMIT(QGeoPositionInfoSource::error(d->error)); |
| 204 |
#endif |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
d->session->updates().position_status.set( |
| 209 |
cllss::Interface::Updates::Status::enabled); |
| 210 |
|
| 211 |
d->session->updates().heading_status.set( |
| 212 |
cllss::Interface::Updates::Status::enabled); |
| 213 |
|
| 214 |
d->session->updates().velocity_status.set( |
| 215 |
cllss::Interface::Updates::Status::enabled); |
| 216 |
|
| 217 |
|
| 218 |
if (m_state != State::one_shot) |
| 219 |
m_state = State::running; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
int core::GeoPositionInfoSource::minimumUpdateInterval() const { |
| 224 |
// We emit our current error state whenever a caller tries to interact |
| 225 |
// with the source although we are in error state. |
| 226 |
if (error() != QGeoPositionInfoSource::NoError) |
| 227 |
{ |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
return 500; |
| 232 |
} |
| 233 |
|
| 234 |
void core::GeoPositionInfoSource::stopUpdates() |
| 235 |
{ |
| 236 |
|
| 237 |
if (error() != QGeoPositionInfoSource::NoError || !d->session) |
| 238 |
{ |
| 239 |
// Don't emit an error from stopUpdates(). Applications usually call |
| 240 |
// stop() in response to an error, thus emitting one here could lead |
| 241 |
// to an infinite call cycle (and maybe stack overflow). |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
d->session->updates().position_status.set( |
| 246 |
cllss::Interface::Updates::Status::disabled); |
| 247 |
|
| 248 |
d->session->updates().heading_status.set( |
| 249 |
cllss::Interface::Updates::Status::disabled); |
| 250 |
|
| 251 |
d->session->updates().velocity_status.set( |
| 252 |
cllss::Interface::Updates::Status::disabled); |
| 253 |
|
| 254 |
m_state = State::stopped; |
| 255 |
} |
| 256 |
|
| 257 |
void core::GeoPositionInfoSource::requestUpdate(int timeout) |
| 258 |
{ |
| 259 |
// We emit our current error state whenever a caller tries to interact |
| 260 |
// with the source although we are in error state. |
| 261 |
if (error() != QGeoPositionInfoSource::NoError) |
| 262 |
{ |
| 263 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 264 |
Q_EMIT(QGeoPositionInfoSource::errorOccurred(d->error)); |
| 265 |
#else |
| 266 |
Q_EMIT(QGeoPositionInfoSource::error(d->error)); |
| 267 |
#endif |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
if (d->timer.isActive()) |
| 272 |
{ |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
// Bump the timeout if caller indicates "choose default value". |
| 277 |
if (timeout <= 0) |
| 278 |
timeout = Private::default_timeout_in_ms; |
| 279 |
|
| 280 |
startUpdates(); |
| 281 |
d->timer.start(timeout); |
| 282 |
} |
| 283 |
|
| 284 |
void core::GeoPositionInfoSource::timeout() |
| 285 |
{ |
| 286 |
// Update timeout reached, clean up |
| 287 |
stopUpdates(); |
| 288 |
|
| 289 |
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 290 |
Q_EMIT(QGeoPositionInfoSource::errorOccurred(QGeoPositionInfoSource::UpdateTimeoutError)); |
| 291 |
#else |
| 292 |
Q_EMIT updateTimeout(); |
| 293 |
#endif |
| 294 |
} |
| 295 |
|
| 296 |
QGeoPositionInfoSource::Error core::GeoPositionInfoSource::error() const |
| 297 |
{ |
| 298 |
return d->error; |
| 299 |
} |
| 300 |
|
| 301 |
// Processes the incoming position update and translates it to Qt world. |
| 302 |
void core::GeoPositionInfoSource::Private::handlePositionUpdate(const cll::Update<cll::Position>& position) |
| 303 |
{ |
| 304 |
QGeoCoordinate coord( |
| 305 |
position.value.latitude.value.value(), |
| 306 |
position.value.longitude.value.value(), |
| 307 |
position.value.altitude ? position.value.altitude->value.value() : 0); |
| 308 |
|
| 309 |
QMutexLocker lock(&lastKnownPositionGuard); |
| 310 |
|
| 311 |
lastKnownPosition.setCoordinate(coord); |
| 312 |
|
| 313 |
if (position.value.accuracy.horizontal) |
| 314 |
{ |
| 315 |
double accuracy = position.value.accuracy.horizontal->value(); |
| 316 |
if (!std::isnan(accuracy)) |
| 317 |
lastKnownPosition.setAttribute(QGeoPositionInfo::HorizontalAccuracy, accuracy); |
| 318 |
} |
| 319 |
|
| 320 |
if (position.value.accuracy.vertical) |
| 321 |
{ |
| 322 |
double accuracy = position.value.accuracy.vertical->value(); |
| 323 |
if (!std::isnan(accuracy)) |
| 324 |
lastKnownPosition.setAttribute(QGeoPositionInfo::VerticalAccuracy, accuracy); |
| 325 |
} |
| 326 |
|
| 327 |
lastKnownPosition.setTimestamp( |
| 328 |
QDateTime::fromMSecsSinceEpoch( |
| 329 |
std::chrono::duration_cast<std::chrono::milliseconds>(position.when.time_since_epoch()).count())); |
| 330 |
|
| 331 |
QGeoPositionInfo info{lastKnownPosition}; |
| 332 |
|
| 333 |
QMetaObject::invokeMethod( |
| 334 |
parent, |
| 335 |
"positionUpdated", |
| 336 |
Qt::QueuedConnection, |
| 337 |
Q_ARG(QGeoPositionInfo, info)); |
| 338 |
|
| 339 |
if (timer.isActive()) |
| 340 |
timer.stop(); |
| 341 |
|
| 342 |
if (parent->m_state == State::one_shot) |
| 343 |
parent->stopUpdates(); |
| 344 |
} |
| 345 |
|
| 346 |
// Processes the incoming heading update and translates it to Qt world. |
| 347 |
void core::GeoPositionInfoSource::Private::handleHeadingUpdate(const cll::Update<cll::Heading>& heading) |
| 348 |
{ |
| 349 |
QMutexLocker lock(&lastKnownPositionGuard); |
| 350 |
|
| 351 |
lastKnownPosition.setAttribute( |
| 352 |
QGeoPositionInfo::Direction, |
| 353 |
heading.value.value()); |
| 354 |
|
| 355 |
lastKnownPosition.setTimestamp( |
| 356 |
QDateTime::fromMSecsSinceEpoch( |
| 357 |
std::chrono::duration_cast<std::chrono::milliseconds>(heading.when.time_since_epoch()).count())); |
| 358 |
|
| 359 |
QGeoPositionInfo info{lastKnownPosition}; |
| 360 |
|
| 361 |
QMetaObject::invokeMethod( |
| 362 |
parent, |
| 363 |
"positionUpdated", |
| 364 |
Qt::QueuedConnection, |
| 365 |
Q_ARG(QGeoPositionInfo, info)); |
| 366 |
} |
| 367 |
|
| 368 |
// Processes the incoming velocity update and translates it to Qt world. |
| 369 |
void core::GeoPositionInfoSource::Private::handleVelocityUpdate(const cll::Update<cll::Velocity>& velocity) |
| 370 |
{ |
| 371 |
QMutexLocker lock(&lastKnownPositionGuard); |
| 372 |
|
| 373 |
lastKnownPosition.setAttribute( |
| 374 |
QGeoPositionInfo::GroundSpeed, |
| 375 |
velocity.value.value()); |
| 376 |
|
| 377 |
lastKnownPosition.setTimestamp( |
| 378 |
QDateTime::fromMSecsSinceEpoch( |
| 379 |
std::chrono::duration_cast<std::chrono::milliseconds>(velocity.when.time_since_epoch()).count())); |
| 380 |
|
| 381 |
QGeoPositionInfo info{lastKnownPosition}; |
| 382 |
|
| 383 |
QMetaObject::invokeMethod( |
| 384 |
parent, |
| 385 |
"positionUpdated", |
| 386 |
Qt::QueuedConnection, |
| 387 |
Q_ARG(QGeoPositionInfo, info)); |
| 388 |
} |
| 389 |
|
| 390 |
void core::GeoPositionInfoSource::Private::createLocationServiceSession() |
| 391 |
{ |
| 392 |
instance = std::make_shared<Instance>(); |
| 393 |
|
| 394 |
try { |
| 395 |
session = instance->getService()->create_session_for_criteria(cll::Criteria{}); |
| 396 |
error = QGeoPositionInfoSource::NoError; |
| 397 |
} catch(...) |
| 398 |
{ |
| 399 |
error = QGeoPositionInfoSource::AccessError; |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
session->updates().position.changed().connect( |
| 404 |
[this](const cll::Update<cll::Position>& new_position) |
| 405 |
{ |
| 406 |
try |
| 407 |
{ |
| 408 |
this->handlePositionUpdate(new_position); |
| 409 |
} catch(...) |
| 410 |
{ |
| 411 |
// We silently ignore the issue and keep going. |
| 412 |
} |
| 413 |
}); |
| 414 |
session->updates().heading.changed().connect( |
| 415 |
[this](const cll::Update<cll::Heading>& new_heading) |
| 416 |
{ |
| 417 |
try |
| 418 |
{ |
| 419 |
this->handleHeadingUpdate(new_heading); |
| 420 |
} catch(...) |
| 421 |
{ |
| 422 |
// We silently ignore the issue and keep going. |
| 423 |
} |
| 424 |
}); |
| 425 |
session->updates().velocity.changed().connect( |
| 426 |
[this](const cll::Update<cll::Velocity>& new_velocity) |
| 427 |
{ |
| 428 |
try |
| 429 |
{ |
| 430 |
this->handleVelocityUpdate(new_velocity); |
| 431 |
} catch(...) |
| 432 |
{ |
| 433 |
// We silently ignore the issue and keep going. |
| 434 |
} |
| 435 |
}); |
| 436 |
} |
| 437 |
|
| 438 |
void core::GeoPositionInfoSource::Private::destroyLocationServiceSession() |
| 439 |
{ |
| 440 |
if (session) |
| 441 |
session = nullptr; |
| 442 |
} |
| 443 |
|
| 444 |
// Creates a new instance and attempts to connect to the background service. |
| 445 |
// Stores errors in the error member. |
| 446 |
core::GeoPositionInfoSource::Private::Private(core::GeoPositionInfoSource* parent) |
| 447 |
: parent(parent), |
| 448 |
session(nullptr), |
| 449 |
error(QGeoPositionInfoSource::NoError) |
| 450 |
{ |
| 451 |
qRegisterMetaType<QGeoPositionInfo>("QGeoPositionInfo"); |
| 452 |
} |
| 453 |
|
| 454 |
core::GeoPositionInfoSource::Private::~Private() |
| 455 |
{ |
| 456 |
destroyLocationServiceSession(); |
| 457 |
} |