1 /*
2  * Copyright (C) 2015 Intel Corporation
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 SENSORS_HAL_HPP
18 #define SENSORS_HAL_HPP
19 
20 #include <hardware/sensors.h>
21 #include <utils/Mutex.h>
22 #include "Sensor.hpp"
23 #include "SensorUtils.hpp"
24 
25 /**
26  * Maximum number of sensor devices
27  */
28 #define MAX_DEVICES  20
29 
30 /**
31  * SensorContext represents the HAL entry class
32  *
33  * The SensorContext class is responsible for initializing
34  * a sensors_poll_device_1_t data structure and exposing the
35  * sensors.h API methods.
36  */
37 class SensorContext {
38   public:
39     /**
40      * Sensor poll device
41      */
42     sensors_poll_device_1_t device;
43 
44     /**
45      * SensorContext constructor
46      */
47     SensorContext(const hw_module_t* module);
48 
49     /**
50      * SensorContext destructor
51      */
52     ~SensorContext();
53 
54     /**
55      * Add sensor module by sensor description & sensor factory function
56      * @param sensorDesc sensor description
57      * @param sensorFactoryFunc sensor factory function
58      * @return 0 if success, error otherwise
59      */
60     static int addSensorModule(struct sensor_t *sensorDesc,
61         Sensor * (*sensorFactoryFunc)(int pollFd));
62 
63     /**
64      * Sensors HAL open wrapper function
65      * @param module hardware module
66      * @param id device identifier
67      * @param device where to store the device address
68      * @return 0 if success, error otherwise
69      */
70     static int OpenWrapper(const struct hw_module_t *module,
71                             const char* id, struct hw_device_t **device);
72     /**
73      * Sensors HAL get_sensors_list wrapper function
74      * @param module sensors module
75      * @param list where to store the list of available sensors
76      * @return 0 if success, error otherwise
77      */
78     static int GetSensorsListWrapper(struct sensors_module_t *module,
79                                       struct sensor_t const **list);
80   private:
81     int activate(int handle, int enabled);
82     int setDelay(int handle, int64_t ns);
83     int pollEvents(sensors_event_t* data, int count);
84     int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
85     int flush(int handle);
86 
87     /*
88      * The wrapper pass through to the specific instantiation of
89      * the SensorContext.
90      */
91     static int CloseWrapper(hw_device_t *dev);
92     static int ActivateWrapper(sensors_poll_device_t *dev, int handle,
93                               int enabled);
94     static int SetDelayWrapper(sensors_poll_device_t *dev, int handle,
95                               int64_t ns);
96     static int PollEventsWrapper(sensors_poll_device_t *dev,
97                                 sensors_event_t *data, int count);
98     static int BatchWrapper(sensors_poll_device_1_t *dev, int handle, int flags,
99                             int64_t period_ns, int64_t timeout);
100     static int FlushWrapper(sensors_poll_device_1_t *dev, int handle);
101 
102     /* Poll file descriptor */
103     int pollFd;
104     /* Array of sensors */
105     Sensor * sensors[MAX_DEVICES];
106 
107     /* Array of sensor factory functions */
108     static Sensor * (*sensorFactoryFuncs[MAX_DEVICES])(int);
109     /* Array of sensor descriptions */
110     static struct sensor_t sensorDescs[MAX_DEVICES];
111     /* Number of registered sensors */
112     static int sensorsNum;
113     /* Mutex used to synchronize the Sensor Context */
114     static android::Mutex mutex;
115 };
116 
117 #endif  // SENSORS_HAL_HPP
118