src/location/dbuscomm_maemo.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 "dbuscomm_maemo_p.h"
       
    43 
       
    44 #include <iostream>
       
    45 using namespace std;
       
    46 
       
    47 QTM_BEGIN_NAMESPACE
       
    48 
       
    49 const QString DBusComm::positioningdService   = QString("com.nokia.positioningd.client");
       
    50 const QString DBusComm::positioningdPath      = QString("/com/nokia/positioningd/client");
       
    51 const QString DBusComm::positioningdInterface = QString("com.nokia.positioningd.client");
       
    52 
       
    53 DBusComm::DBusComm(QObject *parent) : QObject(parent), 
       
    54                                       minimumUpdateInterval(1000), 
       
    55                                       availablePositioningMethods(QGeoPositionInfoSource::AllPositioningMethods)
       
    56 {
       
    57 }
       
    58 
       
    59 
       
    60 int DBusComm::init()
       
    61 {
       
    62     if (!QDBusConnection::sessionBus().isConnected()) {
       
    63         cerr << "Cannot connect to the D-BUS session bus.\n";
       
    64         return -1;
       
    65     }
       
    66 
       
    67     // Application auto-start by dbus may take a while, so try
       
    68     // connecting a few times.
       
    69 
       
    70     int cnt = 6; 
       
    71     do {
       
    72         // cout << "Connecting to positioning daemon..." << endl;
       
    73         positioningdProxy = new QDBusInterface(positioningdService,
       
    74                                                positioningdPath,
       
    75                                                positioningdInterface,
       
    76                                                QDBusConnection::sessionBus());
       
    77         usleep(500000);
       
    78         cnt--;
       
    79     } while (cnt && (positioningdProxy->isValid() == false));
       
    80 
       
    81     if (positioningdProxy->isValid() == false) {
       
    82         cerr << "DBus connection to positioning daemon failed.\n";
       
    83         return -1;
       
    84     }
       
    85     serviceDisconnectWatcher = new QDBusServiceWatcher (positioningdService, QDBusConnection::sessionBus(), 
       
    86                                                         QDBusServiceWatcher::WatchForUnregistration, this);
       
    87     
       
    88     QObject::connect(serviceDisconnectWatcher, SIGNAL(serviceUnregistered ( const QString &)),
       
    89                      this,SLOT(onServiceDisconnect(const QString &)));
       
    90 
       
    91     serviceConnectWatcher = new QDBusServiceWatcher (positioningdService, QDBusConnection::sessionBus(), 
       
    92                                                      QDBusServiceWatcher::WatchForRegistration, this);
       
    93     
       
    94     QObject::connect(serviceConnectWatcher, SIGNAL(serviceRegistered ( const QString &)),
       
    95                      this,SLOT(onServiceConnect(const QString &)));
       
    96     
       
    97 
       
    98     if (createUniqueName() == false) { // set myService, myPath 
       
    99         return -1;
       
   100     }
       
   101 
       
   102     dbusServer = new DBusServer(&serverObj, this);
       
   103     QDBusConnection::sessionBus().registerObject(myPath, &serverObj);
       
   104     if (!QDBusConnection::sessionBus().registerService(myService)) {
       
   105         cerr << qPrintable(QDBusConnection::sessionBus().lastError().message()) << endl;
       
   106         return -1;
       
   107     }
       
   108 
       
   109     sendDBusRegister();
       
   110 
       
   111     return 0;
       
   112 }
       
   113 
       
   114 
       
   115 void DBusComm::onServiceDisconnect(const QString &name)
       
   116 {
       
   117     Q_UNUSED(name);
       
   118     emit serviceDisconnected();
       
   119 }
       
   120 
       
   121 
       
   122 void DBusComm::onServiceConnect(const QString &name)
       
   123 {
       
   124     Q_UNUSED(name);
       
   125     sendDBusRegister();
       
   126     emit serviceConnected();
       
   127 }
       
   128 
       
   129 
       
   130 void DBusComm::receivePositionUpdate(const QGeoPositionInfo &update)
       
   131 {
       
   132     emit receivedPositionUpdate(update);
       
   133 }
       
   134 
       
   135 
       
   136 void DBusComm::receiveSatellitesInView(const QList<QGeoSatelliteInfo> &info)
       
   137 {
       
   138     emit receivedSatellitesInView(info);
       
   139 }
       
   140 
       
   141 
       
   142 void DBusComm::receiveSatellitesInUse(const QList<QGeoSatelliteInfo> &info)
       
   143 {
       
   144     emit receivedSatellitesInUse(info);
       
   145 }
       
   146 
       
   147 
       
   148 void DBusComm::receiveSettings(QGeoPositionInfoSource::PositioningMethod methods, qint32 interval)
       
   149 {
       
   150     availablePositioningMethods = methods;
       
   151     minimumUpdateInterval = interval;
       
   152 }
       
   153 
       
   154 
       
   155 bool DBusComm::sendDBusRegister()
       
   156 {
       
   157     QDBusMessage reply = positioningdProxy->call("registerListener",
       
   158                                                  myService.toAscii().constData(),
       
   159                                                  myPath.toAscii().constData());
       
   160     if (reply.type() == QDBusMessage::ReplyMessage) {
       
   161         QList<QVariant> values = reply.arguments();
       
   162         clientId = values.takeFirst().toInt();
       
   163         quint32 m = values.takeFirst().toUInt();
       
   164         availablePositioningMethods = (QGeoPositionInfoSource::PositioningMethod) m;
       
   165         minimumUpdateInterval = values.takeFirst().toUInt();
       
   166         cout << "Register client ID: " << clientId << endl;
       
   167         cout << "Methods available: " << hex << availablePositioningMethods << dec;
       
   168         cout  << " minInterval: " << minimumUpdateInterval << "\n"; 
       
   169     } else {
       
   170         cerr << endl << "DBus error:\n";
       
   171         cerr << reply.errorName().toAscii().constData() << endl;
       
   172         cerr << reply.errorMessage().toAscii().constData() << endl;
       
   173         return false;
       
   174     }
       
   175 
       
   176     return true;
       
   177 }
       
   178 
       
   179 
       
   180 QGeoPositionInfoSource::PositioningMethods DBusComm::availableMethods() const
       
   181 {
       
   182     return availablePositioningMethods;
       
   183 }
       
   184 
       
   185 
       
   186 int DBusComm::minimumInterval() const
       
   187 {
       
   188     return minimumUpdateInterval;
       
   189 }
       
   190 
       
   191 
       
   192 bool DBusComm::sendConfigRequest(Command command, QGeoPositionInfoSource::PositioningMethods method,
       
   193                                  int interval) const
       
   194 {
       
   195     QDBusReply<int> reply; 
       
   196     reply = positioningdProxy->call("configSession", clientId, command, int(method), interval);
       
   197     
       
   198     //cout << "sessionConfigRequest cmd: cmd:" << command << " method: ";
       
   199     //cout << method << " interval: " << interval << "\n";
       
   200     
       
   201     if (reply.isValid()) {
       
   202         int n = reply.value();
       
   203         cout << "sessionConfigRequest:Reply: " << n << endl;
       
   204     } else {
       
   205         cerr << endl << "DBus error:\n";
       
   206         cerr << reply.error().name().toAscii().constData() << endl;
       
   207         cerr << reply.error().message().toAscii().constData() << endl;
       
   208         return false;
       
   209     }
       
   210 
       
   211     return true;
       
   212 }
       
   213 
       
   214 
       
   215 QGeoPositionInfo& DBusComm::requestLastKnownPosition(bool satelliteMethodOnly)
       
   216 {
       
   217     QDBusReply<QByteArray> reply; 
       
   218     reply = positioningdProxy->call("latestPosition", satelliteMethodOnly);
       
   219     static QGeoPositionInfo update;
       
   220 
       
   221     if (reply.isValid()) {
       
   222         // cout << "requestLastKnownPosition(): received update\n";
       
   223         QByteArray message = reply.value();
       
   224         QDataStream stream(message);
       
   225         stream >> update;
       
   226     } else {
       
   227         cerr << endl << "DBus error:\n";
       
   228         cerr << reply.error().name().toAscii().constData() << endl;
       
   229         cerr << reply.error().message().toAscii().constData() << endl;
       
   230         update = QGeoPositionInfo();
       
   231     }
       
   232 
       
   233     return update;
       
   234 } 
       
   235 
       
   236 
       
   237 bool DBusComm::createUniqueName()
       
   238 {
       
   239     QFile uuidfile("/proc/sys/kernel/random/uuid");
       
   240     if (!uuidfile.open(QIODevice::ReadOnly)) {
       
   241         cerr << "UUID file failed.";
       
   242         return false;
       
   243     }
       
   244 
       
   245     QTextStream in(&uuidfile);
       
   246     QString uuid = 'I' + in.readLine();
       
   247     uuid.replace('-', 'I');
       
   248     myService   = "com.nokia.qlocation." + uuid;
       
   249     myPath      = "/com/nokia/qlocation/" + uuid;
       
   250 
       
   251     return true;
       
   252 }
       
   253 
       
   254 #include "moc_dbuscomm_maemo_p.cpp"
       
   255 QTM_END_NAMESPACE
       
   256