Commit 8fab5538 authored by spiiroin's avatar spiiroin

[hybrisadaptor] Refactor sensor start/stop logic

Whether sensors should be started or stopped depends on a number
of state variables. Using case specific conditionals at places
where state variables might change makes the code fragile and
hard to maintain.

Separate sensor hal operations more clearly from logic and forms
dictated by sensorfwd conventions.

Use m_memberName naming convention for member variables, make
all member data private and usable only via accessor functions.

Use a single sensor start/stop policy evaluation function and
call it from each place where relevant state variables might
change.

Use sensorfwd specific logging functions instead of qDebug etc.
Signed-off-by: spiiroin's avatarSimo Piiroinen <simo.piiroinen@jollamobile.com>
parent 4fb14616
......@@ -138,7 +138,7 @@ void HybrisProximityAdaptor::processSample(const sensors_event_t& data)
ProximityData *d = buffer->nextSlot();
d->timestamp_ = quint64(data.timestamp * .001);
bool near = false;
if (data.distance < maxRange) {
if (data.distance < maxRange()) {
near = true;
}
d->withinProximity_ = near;
......
This diff is collapsed.
......@@ -34,50 +34,68 @@
class HybrisAdaptor;
struct HybrisSensorState
{
HybrisSensorState();
~HybrisSensorState();
int m_minDelay;
int m_maxDelay;
int m_delay;
int m_active;
};
class HybrisManager : public QObject
{
Q_OBJECT
public:
explicit HybrisManager(QObject *parent = 0);
static HybrisManager *instance();
virtual ~HybrisManager();
int handleForType(int sensorType);
int maxRange(int sensorType);
int minDelay(int sensorType);
int resolution(int sensorType);
bool setDelay(int handle, int interval);
void startReader(HybrisAdaptor *adaptor);
void stopReader(HybrisAdaptor *adaptor);
bool resumeReader(HybrisAdaptor *adaptor);
void standbyReader(HybrisAdaptor *adaptor);
bool openSensors();
bool closeSensors();
void registerAdaptor(HybrisAdaptor * adaptor);
void processSample(const sensors_event_t& data);
explicit HybrisManager(QObject *parent = 0);
virtual ~HybrisManager();
protected:
// methods
void init();
void closeAllSensors();
/* - - - - - - - - - - - - - - - - - - - *
* android sensor hal functions
* - - - - - - - - - - - - - - - - - - - */
int halIndexForHandle(int handle) const;
int halIndexForType (int sensorType) const;
int halHandleForType (int sensorType) const;
float halGetMaxRange (int handle) const;
float halGetResolution (int handle) const;
int halGetMinDelay (int handle) const;
int halGetMaxDelay (int handle) const;
int halGetDelay (int handle) const;
bool halSetDelay (int handle, int delay_ms);
bool halGetActive (int handle) const;
bool halSetActive (int handle, bool active);
/* - - - - - - - - - - - - - - - - - - - *
* HybrisManager <--> sensorfwd
* - - - - - - - - - - - - - - - - - - - */
void startReader (HybrisAdaptor *adaptor);
void stopReader (HybrisAdaptor *adaptor);
void registerAdaptor (HybrisAdaptor * adaptor);
void processSample (const sensors_event_t& data);
private:
// fields
struct sensors_poll_device_t* device;
struct sensor_t const* sensorList;
struct sensors_module_t* module;
int sensorsCount;
QMap <int, int> sensorMap; //type, index
QMap <int, HybrisAdaptor *> registeredAdaptors; //type, obj
pthread_t adaptorReaderTid;
bool m_initialized;
QMap <int, HybrisAdaptor *> m_registeredAdaptors; // type -> obj
struct sensors_module_t *m_halModule;
struct sensors_poll_device_t *m_halDevice;
int m_halSensorCount;
const struct sensor_t *m_halSensorArray; // [m_halSensorCount]
HybrisSensorState *m_halSensorState; // [m_halSensorCount]
QMap <int, int> m_halIndexOfType; // type -> index
QMap <int, int> m_halIndexOfHandle; // handle -> index
pthread_t m_halEventReaderTid;
friend class HybrisAdaptorReader;
private:
static void *adaptorReaderThread(void *aptr);
static void *halEventReaderThread(void *aptr);
};
class HybrisAdaptor : public DeviceAdaptor
......@@ -87,47 +105,44 @@ public:
virtual ~HybrisAdaptor();
virtual void init();
bool addSensorType(int type);
virtual bool startAdaptor();
bool isRunning() const;
bool isRunning() const;
virtual void stopAdaptor();
void evaluateSensor();
virtual bool startSensor();
virtual void stopSensor();
virtual bool standby();
virtual bool resume();
qreal maxRange;
qint32 minDelay;
qreal resolution;
virtual void sendInitialData();
friend class HybrisManager;
int sensorHandle;
int sensorType;
int cachedInterval;
virtual void sendInitialData() {}
protected:
virtual void processSample(const sensors_event_t& data) = 0;
qreal minRange() const;
qreal maxRange() const;
qreal resolution() const;
unsigned int minInterval() const;
unsigned int maxInterval() const;
virtual unsigned int interval() const;
virtual bool setInterval(const unsigned int value, const int sessionId);
virtual unsigned int evaluateIntervalRequests(int& sessionId) const;
static bool writeToFile(const QByteArray& path, const QByteArray& content);
private:
void stopReaderThread();
bool startReaderThread();
QList<int> sensorIds;
unsigned int interval_;
bool inStandbyMode_;
volatile bool running_;
bool shouldBeRunning_;
bool m_inStandbyMode;
volatile bool m_isRunning;
bool m_shouldBeRunning;
int m_sensorHandle;
int m_sensorType;
};
#endif // HybrisAdaptor_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment