OSDN Git Service

libsensors: Added support for Proximity sensor
[android-x86/hardware-intel-libsensors.git] / SensorBase.h
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
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
17 #ifndef ANDROID_SENSOR_BASE_H
18 #define ANDROID_SENSOR_BASE_H
19
20 #include <string>
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <sys/cdefs.h>
25 #include <sys/types.h>
26 #include <hardware/sensors.h>
27
28 #include "InputEventReader.h"
29
30 struct sensors_event_t;
31
32 /**
33  * The base class defines how it's supposed to be used by sensors.cpp.
34  * Never use this class directly for sensor implementations. A new
35  * Sensor<DevType>Dev class must be created.
36  *
37  * This class does opening & closing of mDevPath which is supposed to be
38  * set in the constructor. Open happens on enable(1) & close on enable(0).
39  *
40  * The sensor is closed & disabled when mFd is -1;
41  *
42  * Sensor implementations/Sensor<Type>Dev classes must set mPendingEvent
43  * flag to true/false by themselves in the readEvent loop.
44  */
45 class SensorBase {
46
47 protected:
48     std::string mDevPath;
49     int mFd;
50     bool mHasPendingEvent;
51     bool mEnabled;
52     sensors_event_t mPendingEvent;
53
54     static int64_t getTimestamp();
55     static int64_t timevalToNano(timeval const& t) {
56         return t.tv_sec*1000000000LL + t.tv_usec*1000;
57     }
58
59     virtual int setInitialState() { return 0; };
60
61 public:
62     SensorBase():
63         mFd(-1),
64         mHasPendingEvent(false),
65         mEnabled(false) {};
66
67     SensorBase(std::string dev_path):
68         mDevPath(dev_path),
69         mFd(-1),
70         mHasPendingEvent(false),
71         mEnabled(false) {};
72
73     virtual ~SensorBase() { close(); };
74     virtual int open();
75     virtual void close();
76     virtual int enable(int enabled) = 0;
77
78     virtual int readEvents(sensors_event_t *data, int count) = 0;
79     virtual int setDelay(int64_t ns) = 0;
80     virtual int discover() { return 0; }
81
82     int fd() const { return mFd; };
83     virtual bool hasPendingEvents() const { return mHasPendingEvent; };
84 };
85 #endif  // ANDROID_SENSOR_BASE_H