File indexing completed on 2024-05-19 05:37:50

0001 /*
0002     SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "location_gps.h"
0008 #include "geolocdebug.h"
0009 
0010 Gpsd::Gpsd(gps_data_t *gpsdata)
0011     : m_gpsdata(gpsdata)
0012     , m_abort(false)
0013 {
0014 }
0015 
0016 Gpsd::~Gpsd()
0017 {
0018     m_abort = true;
0019     m_condition.wakeOne();
0020     wait();
0021 }
0022 
0023 void Gpsd::update()
0024 {
0025     if (!isRunning()) {
0026         start();
0027     } else {
0028         m_condition.wakeOne();
0029     }
0030 }
0031 
0032 void Gpsd::run()
0033 {
0034 #if defined(GPSD_API_MAJOR_VERSION) && (GPSD_API_MAJOR_VERSION >= 3) && defined(WATCH_ENABLE)
0035     gps_stream(m_gpsdata, WATCH_ENABLE, nullptr);
0036 #else
0037     gps_query(m_gpsdata, "w+x\n");
0038 #endif
0039 
0040     while (!m_abort) {
0041         Plasma5Support::DataEngine::Data d;
0042 
0043 #if GPSD_API_MAJOR_VERSION >= 7
0044         if (gps_read(m_gpsdata, NULL, 0) != -1) {
0045 #elif GPSD_API_MAJOR_VERSION >= 5
0046         if (gps_read(m_gpsdata) != -1) {
0047 #else
0048         if (gps_poll(m_gpsdata) != -1) {
0049 #endif
0050 #if GPSD_API_MAJOR_VERSION >= 9
0051             if (m_gpsdata->online.tv_sec || m_gpsdata->online.tv_nsec) {
0052 #else
0053             if (m_gpsdata->online) {
0054 #endif
0055 #if defined(STATUS_UNK) // STATUS_NO_FIX was renamed to STATUS_UNK without bumping API version
0056                 if (m_gpsdata->fix.status != STATUS_UNK) {
0057 #elif GPSD_API_MAJOR_VERSION >= 10
0058                 if (m_gpsdata->fix.status != STATUS_NO_FIX) {
0059 #else
0060             if (m_gpsdata->status != STATUS_NO_FIX) {
0061 #endif
0062                     d["accuracy"] = 30;
0063                     d["latitude"] = QString::number(m_gpsdata->fix.latitude);
0064                     d["longitude"] = QString::number(m_gpsdata->fix.longitude);
0065                 }
0066             }
0067         }
0068 
0069         Q_EMIT dataReady(d);
0070 
0071         m_condition.wait(&m_mutex);
0072     }
0073 }
0074 
0075 Gps::Gps(QObject *parent)
0076     : GeolocationProvider(parent)
0077     , m_gpsd(nullptr)
0078 #if GPSD_API_MAJOR_VERSION >= 5
0079     , m_gpsdata(nullptr)
0080 #endif
0081 {
0082 #if GPSD_API_MAJOR_VERSION >= 5
0083     m_gpsdata = new gps_data_t;
0084     if (gps_open("localhost", DEFAULT_GPSD_PORT, m_gpsdata) != -1) {
0085 #else
0086     gps_data_t *m_gpsdata = gps_open("localhost", DEFAULT_GPSD_PORT);
0087     if (m_gpsdata) {
0088 #endif
0089         qCDebug(DATAENGINE_GEOLOCATION) << "gpsd found.";
0090         m_gpsd = new Gpsd(m_gpsdata);
0091         connect(m_gpsd, SIGNAL(dataReady(Plasma5Support::DataEngine::Data)), this, SLOT(setData(Plasma5Support::DataEngine::Data)));
0092     } else {
0093         qCWarning(DATAENGINE_GEOLOCATION) << "gpsd not found";
0094     }
0095 
0096     setIsAvailable(m_gpsd);
0097 }
0098 
0099 Gps::~Gps()
0100 {
0101     delete m_gpsd;
0102 #if GPSD_API_MAJOR_VERSION >= 5
0103     delete m_gpsdata;
0104 #endif
0105 }
0106 
0107 void Gps::update()
0108 {
0109     if (m_gpsd) {
0110         m_gpsd->update();
0111     }
0112 }
0113 
0114 K_PLUGIN_CLASS(Gps)
0115 
0116 #include "location_gps.moc"