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 mMpuAccuracy; //global storage for the current accuracy status
91    int mNewData; //flag indicating that the MPL calculated new output values
92    int mDmpStarted;
93    long mMasterSensorMask;
94    long mLocalSensorMask;
95    int mPollTime;
96    int mCurFifoRate; //current fifo rate
97    bool mHaveGoodMpuCal; //flag indicating that the cal file can be written
98    bool mHaveGoodCompassCal;
99    bool mUseTimerIrqAccel;
100    bool mUsetimerIrqCompass;
101    bool mUseTimerirq;
102    struct pollfd mPollFds[4];
103    int mSampleCount;
104    pthread_mutex_t mMplMutex;
105    int64_t now_ns();
106    int64_t select_ns(unsigned long long time_set[]);
107
108    enum FILEHANDLES
109    {
110        MPUIRQ_FD, ACCELIRQ_FD, COMPASSIRQ_FD, TIMERIRQ_FD,
111    };
112
113private:
114
115    int update_delay();
116    int accel_fd;
117    int timer_fd;
118
119    uint32_t mEnabled;
120    uint32_t mPendingMask;
121    sensors_event_t mPendingEvents[numSensors];
122    uint64_t mDelays[numSensors];
123    hfunc_t mHandlers[numSensors];
124    bool mForceSleep;
125    long int mOldEnabledMask;
126    android::KeyedVector<int, int> mIrqFds;
127
128    /* added for dynamic get sensor list              */
129    bool mNineAxisEnabled;
130    void fillAccel(unsigned char accel, struct sensor_t *list);
131    void fillCompass(unsigned char compass, struct sensor_t *list);
132    void fillGyro(const char* gyro, struct sensor_t *list);
133    void fillRV(struct sensor_t *list);
134    void fillOrientation(struct sensor_t *list);
135    void fillGravity(struct sensor_t *list);
136    void fillLinearAccel(struct sensor_t *list);
137};
138
139void setCallbackObject(MPLSensor*);
140
141/*****************************************************************************/
142
143#endif  // ANDROID_MPL_SENSOR_H
144