1/*
2 * Copyright (C) 2011 Invensense, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/*************Removed the gesture related info for Google check in : Meenakshi Ramamoorthi: May 31st *********/
17
18#ifndef ANDROID_MPL_SENSOR_H
19#define ANDROID_MPL_SENSOR_H
20
21#include <stdint.h>
22#include <errno.h>
23#include <sys/cdefs.h>
24#include <sys/types.h>
25#include <poll.h>
26#include <utils/Vector.h>
27#include <utils/KeyedVector.h>
28#include "sensors.h"
29#include "SensorBase.h"
30
31/*****************************************************************************/
32/** MPLSensor implementation which fits into the HAL example for crespo provided
33 * * by Google.
34 * * WARNING: there may only be one instance of MPLSensor, ever.
35 */
36
37class MPLSensor: public SensorBase
38{
39    typedef void (MPLSensor::*hfunc_t)(sensors_event_t*, uint32_t*, int);
40
41public:
42    MPLSensor();
43    virtual ~MPLSensor();
44
45    enum
46    {
47        Gyro=0,
48        Accelerometer,
49        MagneticField,
50        Orientation,
51        RotationVector,
52        LinearAccel,
53        Gravity,
54        numSensors
55    };
56
57    virtual int setDelay(int32_t handle, int64_t ns);
58    virtual int enable(int32_t handle, int enabled);
59    virtual int readEvents(sensors_event_t *data, int count);
60    virtual int getFd() const;
61    virtual int getAccelFd() const;
62    virtual int getTimerFd() const;
63    virtual int getPowerFd() const;
64    virtual int getPollTime();
65    virtual bool hasPendingEvents() const;
66    virtual void handlePowerEvent();
67    virtual void sleepEvent();
68    virtual void wakeEvent();
69    int populateSensorList(struct sensor_t *list, int len);
70    void cbOnMotion(uint16_t);
71    void cbProcData();
72
73protected:
74
75    void clearIrqData(bool* irq_set);
76    void setPowerStates(int enabledsensor);
77    void initMPL();
78    void setupFIFO();
79    void setupCallbacks();
80    void gyroHandler(sensors_event_t *data, uint32_t *pendmask, int index);
81    void accelHandler(sensors_event_t *data, uint32_t *pendmask, int index);
82    void compassHandler(sensors_event_t *data, uint32_t *pendmask, int index);
83    void rvHandler(sensors_event_t *data, uint32_t *pendmask, int index);
84    void laHandler(sensors_event_t *data, uint32_t *pendmask, int index);
85    void gravHandler(sensors_event_t *data, uint32_t *pendmask, int index);
86    void orienHandler(sensors_event_t *data, uint32_t *pendmask, int index);
87    void calcOrientationSensor(float *Rx, float *Val);
88    int estimateCompassAccuracy();
89
90    int mNewData; //flag indicating that the MPL calculated new output values
91    int mDmpStarted;
92    long mMasterSensorMask;
93    long mLocalSensorMask;
94    int mPollTime;
95    int mCurFifoRate; //current fifo rate
96    bool mHaveGoodMpuCal; //flag indicating that the cal file can be written
97    bool mHaveGoodCompassCal;
98    bool mUseTimerIrqAccel;
99    bool mUsetimerIrqCompass;
100    bool mUseTimerirq;
101    struct pollfd mPollFds[4];
102    int mSampleCount;
103    pthread_mutex_t mMplMutex;
104    int64_t now_ns();
105    int64_t select_ns(unsigned long long time_set[]);
106
107    enum FILEHANDLES
108    {
109        MPUIRQ_FD, ACCELIRQ_FD, COMPASSIRQ_FD, TIMERIRQ_FD,
110    };
111
112private:
113
114    int update_delay();
115    int accel_fd;
116    int timer_fd;
117
118    uint32_t mEnabled;
119    uint32_t mPendingMask;
120    sensors_event_t mPendingEvents[numSensors];
121    uint64_t mDelays[numSensors];
122    hfunc_t mHandlers[numSensors];
123    bool mForceSleep;
124    long int mOldEnabledMask;
125    android::KeyedVector<int, int> mIrqFds;
126
127    /* added for dynamic get sensor list              */
128    bool mNineAxisEnabled;
129    void fillAccel(unsigned char accel, struct sensor_t *list);
130    void fillCompass(unsigned char compass, struct sensor_t *list);
131    void fillGyro(const char* gyro, struct sensor_t *list);
132    void fillRV(struct sensor_t *list);
133    void fillOrientation(struct sensor_t *list);
134    void fillGravity(struct sensor_t *list);
135    void fillLinearAccel(struct sensor_t *list);
136};
137
138void setCallbackObject(MPLSensor*);
139
140/*****************************************************************************/
141
142#endif  // ANDROID_MPL_SENSOR_H
143