2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_SENSOR_BASE_H
18 #define ANDROID_SENSOR_BASE_H
24 #include <sys/cdefs.h>
25 #include <sys/types.h>
26 #include <hardware/sensors.h>
28 #include "InputEventReader.h"
30 struct sensors_event_t;
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.
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).
40 * The sensor is closed & disabled when mFd is -1;
42 * Sensor implementations/Sensor<Type>Dev classes must set mPendingEvent
43 * flag to true/false by themselves in the readEvent loop.
50 bool mHasPendingEvent;
52 sensors_event_t mPendingEvent;
54 static int64_t getTimestamp();
55 static int64_t timevalToNano(timeval const& t) {
56 return t.tv_sec*1000000000LL + t.tv_usec*1000;
59 virtual int setInitialState() { return 0; };
64 mHasPendingEvent(false),
67 SensorBase(std::string dev_path):
70 mHasPendingEvent(false),
73 virtual ~SensorBase() { close(); };
76 virtual int enable(int enabled) = 0;
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; }
82 int fd() const { return mFd; };
83 virtual bool hasPendingEvents() const { return mHasPendingEvent; };
85 #endif // ANDROID_SENSOR_BASE_H