qtmobility/src/sensors/qsensormanager.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    42 #include "qsensormanager.h"
    42 #include "qsensormanager.h"
    43 #include <QDebug>
    43 #include <QDebug>
    44 #include "qsensorpluginloader_p.h"
    44 #include "qsensorpluginloader_p.h"
    45 #include "qsensorplugin.h"
    45 #include "qsensorplugin.h"
    46 #include <QSettings>
    46 #include <QSettings>
    47 
    47 #include "sensorlog_p.h"
    48 //#define LOG() if (1); else qDebug()
       
    49 
    48 
    50 QTM_BEGIN_NAMESPACE
    49 QTM_BEGIN_NAMESPACE
    51 
    50 
    52 Q_GLOBAL_STATIC_WITH_ARGS(QSensorPluginLoader, pluginLoader, (QSensorPluginInterface_iid, QLatin1String("/sensors")))
    51 Q_GLOBAL_STATIC_WITH_ARGS(QSensorPluginLoader, pluginLoader, (QSensorPluginInterface_iid, QLatin1String("/sensors")))
    53 
    52 
    78 static void loadPlugins()
    77 static void loadPlugins()
    79 {
    78 {
    80     QSensorManagerPrivate *d = sensorManagerPrivate();
    79     QSensorManagerPrivate *d = sensorManagerPrivate();
    81     d->pluginsLoaded = true;
    80     d->pluginsLoaded = true;
    82 
    81 
    83     qDebug() << "initializing static plugins";
    82     SENSORLOG() << "initializing static plugins";
    84     Q_FOREACH (CreatePluginFunc func, d->staticRegistrations) {
    83     Q_FOREACH (CreatePluginFunc func, d->staticRegistrations) {
    85         QSensorPluginInterface *plugin = func();
    84         QSensorPluginInterface *plugin = func();
    86         plugin->registerSensors();
    85         plugin->registerSensors();
    87     }
    86     }
    88 
    87 
    89     qDebug() << "initializing plugins";
    88     SENSORLOG() << "initializing plugins";
    90     Q_FOREACH (QSensorPluginInterface *plugin, pluginLoader()->plugins()) {
    89     Q_FOREACH (QSensorPluginInterface *plugin, pluginLoader()->plugins()) {
    91         plugin->registerSensors();
    90         plugin->registerSensors();
    92     }
    91     }
    93 }
    92 }
    94 
    93 
    97 /*!
    96 /*!
    98     \class QSensorManager
    97     \class QSensorManager
    99     \ingroup sensors_backend
    98     \ingroup sensors_backend
   100 
    99 
   101     \preliminary
   100     \preliminary
   102     \brief The QSensorManager class returns the sensors on a device.
   101     \brief The QSensorManager class handles registration and creation of sensor backends.
   103 
   102 
   104     A given device will have a variety of sensors. The sensors are
   103     Sensor plugins register backends using the registerBackend() function.
   105     categorized by type.
   104 
       
   105     When QSensor::connectToBackend() is called, the createBackend() function will be called.
   106 */
   106 */
   107 
   107 
   108 /*!
   108 /*!
   109     Register a sensor for \a type. The \a identifier must be unique.
   109     Register a sensor for \a type. The \a identifier must be unique.
   110 
   110 
   115     QSensorManagerPrivate *d = sensorManagerPrivate();
   115     QSensorManagerPrivate *d = sensorManagerPrivate();
   116     if (!d->backendsByType.contains(type)) {
   116     if (!d->backendsByType.contains(type)) {
   117         (void)d->backendsByType[type];
   117         (void)d->backendsByType[type];
   118         d->firstIdentifierForType[type] = identifier;
   118         d->firstIdentifierForType[type] = identifier;
   119     }
   119     }
   120     qDebug() << "registering backend for type" << type << "identifier" << identifier;// << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   120     SENSORLOG() << "registering backend for type" << type << "identifier" << identifier;// << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   121     FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[type];
   121     FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[type];
   122     factoryByIdentifier[identifier] = factory;
   122     factoryByIdentifier[identifier] = factory;
   123 }
   123 }
   124 
   124 
   125 /*!
   125 /*!
   140 
   140 
   141     QSensorManagerPrivate *d = sensorManagerPrivate();
   141     QSensorManagerPrivate *d = sensorManagerPrivate();
   142     if (!d->pluginsLoaded)
   142     if (!d->pluginsLoaded)
   143         loadPlugins();
   143         loadPlugins();
   144 
   144 
   145     //LOG() << "QSensorManager::createBackend" << "type" << sensor->type() << "identifier" << sensor->identifier();
   145     SENSORLOG() << "QSensorManager::createBackend" << "type" << sensor->type() << "identifier" << sensor->identifier();
   146 
   146 
   147     if (!d->backendsByType.contains(sensor->type())) {
   147     if (!d->backendsByType.contains(sensor->type())) {
   148         qDebug() << "no backends of type" << sensor->type() << "have been registered.";
   148         SENSORLOG() << "no backends of type" << sensor->type() << "have been registered.";
   149         return 0;
   149         return 0;
   150     }
   150     }
   151 
   151 
   152     const FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[sensor->type()];
   152     const FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[sensor->type()];
   153     QSensorBackendFactory *factory;
   153     QSensorBackendFactory *factory;
   154     QSensorBackend *backend;
   154     QSensorBackend *backend;
   155 
   155 
   156     if (sensor->identifier().isEmpty()) {
   156     if (sensor->identifier().isEmpty()) {
   157         QByteArray defaultIdentifier = QSensor::defaultSensorForType(sensor->type());
   157         QByteArray defaultIdentifier = QSensor::defaultSensorForType(sensor->type());
   158         //LOG() << "Trying the default" << defaultIdentifier;
   158         SENSORLOG() << "Trying the default" << defaultIdentifier;
   159         // No identifier set, try the default
   159         // No identifier set, try the default
   160         factory = factoryByIdentifier[defaultIdentifier];
   160         factory = factoryByIdentifier[defaultIdentifier];
   161         //LOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   161         //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   162         sensor->setIdentifier(defaultIdentifier); // the factory requires this
   162         sensor->setIdentifier(defaultIdentifier); // the factory requires this
   163         backend = factory->createBackend(sensor);
   163         backend = factory->createBackend(sensor);
   164         if (backend) return backend; // Got it!
   164         if (backend) goto gotbackend; // Got it!
   165 
   165 
   166         // The default failed to instantiate so try any other registered sensors for this type
   166         // The default failed to instantiate so try any other registered sensors for this type
   167         Q_FOREACH (const QByteArray &identifier, factoryByIdentifier.keys()) {
   167         Q_FOREACH (const QByteArray &identifier, factoryByIdentifier.keys()) {
   168             //LOG() << "Trying" << identifier;
   168             SENSORLOG() << "Trying" << identifier;
   169             if (identifier == defaultIdentifier) continue; // Don't do the default one again
   169             if (identifier == defaultIdentifier) continue; // Don't do the default one again
   170             factory = factoryByIdentifier[identifier];
   170             factory = factoryByIdentifier[identifier];
   171             //LOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   171             //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   172             sensor->setIdentifier(identifier); // the factory requires this
   172             sensor->setIdentifier(identifier); // the factory requires this
   173             backend = factory->createBackend(sensor);
   173             backend = factory->createBackend(sensor);
   174             if (backend) return backend; // Got it!
   174             if (backend) goto gotbackend; // Got it!
   175         }
   175         }
   176         //LOG() << "FAILED";
   176         SENSORLOG() << "FAILED";
   177         sensor->setIdentifier(QByteArray()); // clear the identifier
   177         sensor->setIdentifier(QByteArray()); // clear the identifier
   178     } else {
   178     } else {
   179         if (!factoryByIdentifier.contains(sensor->identifier())) {
   179         if (!factoryByIdentifier.contains(sensor->identifier())) {
   180             qDebug() << "no backend with identifier" << sensor->identifier() << "for type" << sensor->type();
   180             SENSORLOG() << "no backend with identifier" << sensor->identifier() << "for type" << sensor->type();
   181             return 0;
   181             return 0;
   182         }
   182         }
   183 
   183 
   184         // We were given an explicit identifier so don't substitute other backends if it fails to instantiate
   184         // We were given an explicit identifier so don't substitute other backends if it fails to instantiate
   185         factory = factoryByIdentifier[sensor->identifier()];
   185         factory = factoryByIdentifier[sensor->identifier()];
   186         //LOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   186         //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory);
   187         backend = factory->createBackend(sensor);
   187         backend = factory->createBackend(sensor);
   188         if (backend) return backend; // Got it!
   188         if (backend) goto gotbackend; // Got it!
   189     }
   189     }
   190 
   190 
   191     qDebug() << "no suitable backend found for requested identifier" << sensor->identifier() << "and type" << sensor->type();
   191     SENSORLOG() << "no suitable backend found for requested identifier" << sensor->identifier() << "and type" << sensor->type();
   192     return 0;
   192     return 0;
       
   193 
       
   194 gotbackend:
       
   195     if (sensor->availableDataRates().count() == 0) {
       
   196         qWarning() << sensor->identifier() << "backend does not support any data rates. It cannot be used.";
       
   197     }
       
   198     if (sensor->dataRate() == 0) {
       
   199         qWarning() << sensor->identifier() << "backend did not supply default data rate.";
       
   200     }
       
   201     return backend;
   193 }
   202 }
   194 
   203 
   195 // =====================================================================
   204 // =====================================================================
   196 
   205 
   197 /*!
   206 /*!
   213 QList<QByteArray> QSensor::sensorsForType(const QByteArray &type)
   222 QList<QByteArray> QSensor::sensorsForType(const QByteArray &type)
   214 {
   223 {
   215     QSensorManagerPrivate *d = sensorManagerPrivate();
   224     QSensorManagerPrivate *d = sensorManagerPrivate();
   216     if (!d->pluginsLoaded)
   225     if (!d->pluginsLoaded)
   217         loadPlugins();
   226         loadPlugins();
       
   227 
       
   228     // no sensors of that type exist
       
   229     if (!d->backendsByType.contains(type))
       
   230         return QList<QByteArray>();
   218 
   231 
   219     return d->backendsByType[type].keys();
   232     return d->backendsByType[type].keys();
   220 }
   233 }
   221 
   234 
   222 /*!
   235 /*!